> For the complete documentation index, see [llms.txt](https://bayun.gitbook.io/bayuncoresdk-javascript-programming-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bayun.gitbook.io/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations/lock-unlock-binary-data.md).

# 6.2 Lock/Unlock File Text

### 6.2.1 Read a file as text&#x20;

The locked/unlocked file can be read as text as shown in the code snippet.&#x20;

{% tabs %}
{% tab title="JavaScript" %}

```javascript
let fileInfoObject = { "fileText":"", "metaData":"" }

function readFileAsText(){
  var file = document.getElementById("file").files[0];
  var reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = function () {
    var fileData=reader.result;
    fileMetadata=fileData.split(",")[0]; //fileMetadata is prepend to the file text and is separated using a comma(,)
    fileText = fileData.replace(fileMetadata+",",""); // Remove fileMetadata along with ","
    fileInfoObject = {
        "fileText":fileText,
        "metaData":fileMetadata
      }
    };
}

```

{% endtab %}
{% endtabs %}

### 6.2.2 Write fileText to a file

The locked/unlocked file text can be written back to the file as shown in the code snippet.&#x20;

{% tabs %}
{% tab title="JavaScript" %}

```javascript
function writeFileTextToFile(fileText, fileMetadata){
  var a = document.createElement("a"); // Creates <a>
  a.href = fileMetadata + "," + fileText;
  var extension = fileMetadata.substring(fileMetadata.indexOf('/')+1,fileMetadata.indexOf(';'));
  console.log("extension",extension);
  a.download = "<YourFileName>" + "." + extension; //File name Here
  a.click();
}
```

{% endtab %}
{% endtabs %}

### 6.2.3 Lock File Text

The `lockFileText` function locks a file data as text with default [BayunEncryptionPolicy](/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations.md#encryption-policy) and [BayunKeyGenerationPolicy](/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations.md#key-generation-policy) dictated by company settings.

The function takes the following parameters :

* **sessionId** : Unique SessionId which is received in the login/registration function response.
* **fileText** : File text to be locked. You can use the utility function [readFileAsText](https://bayun.gitbook.io/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations/lock-unlock-binary-data#5.2.1-read-a-file-as-text) to read file as file text.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
readFileAsText();

var lockedFileText = await bayunCore.lockFileText({
  sessionId:"<sessionId>",
  text: fileInfoObject.fileText,
});

writeFileTextToFile(lockedFileText, fileInfoObject.metaData); // To write lockedFileText to the file.
```

{% endtab %}
{% endtabs %}

### 6.2.4 Lock File Text with Encryption Policy, Key Generation Policy

The `lockFileText` function with encryption policy, key generation policy as parameters locks a file data as text. The function takes the following parameters :

* **sessionId** : Unique SessionId which is received in the login/registration function response.
* **fileText** : File text to be locked. You can use the utility function [readFileAsText](https://bayun.gitbook.io/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations/lock-unlock-binary-data#5.2.1-read-a-file-as-text) to read file as file text.
* **encryptionPolicy** : [BayunEncryptionPolicy](/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations.md#encryption-policy) determines the key to be used to generate the lockingKey.
* **keyGenerationPolicy** : [BayunKeyGenerationPolicy](/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations.md#key-generation-policy) determines the policy to generate the lockingKey.
* **groupId** : GroupId is required if encryptionPolicy is `GROUP`.

{% hint style="info" %}
If encryptionPolicy is other than `GROUP` then groupId should be an empty string.
{% endhint %}

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const encryptionPolicy = BayunCore.EncryptionPolicy.GROUP;
const keyGenerationPolicy = BayunCore.KeyGenerationPolicy.ENVELOPE;

readFileAsText();

var lockedFileText = await bayunCore.lockFileText(
  sessionId: "<sessionId>",
  text: fileInfoObject.fileText,
  encryptionPolicy: encryptionPolicy,
  keyGenerationPolicy: keyGenerationPolicy,
  groupId:"<groupId>",
);

writeFileTextToFile(lockedFileText, fileInfoObject.metaData); // To write lockedFileText to the file.
```

{% endtab %}
{% endtabs %}

### 6.2.5 Unlock File Text

The `unlockFileText` function unlocks a locked file data as text. The function takes the following parameters :

* **sessionId** : Unique SessionId which is received in the login/registration function response.
* **fileText** : File text to be unlocked. You can use the utility function [readFileAsText](https://bayun.gitbook.io/bayuncoresdk-javascript-programming-guide/bayuncoresdk-operations/lock-unlock-binary-data#5.2.1-read-a-file-as-text) to read locked file as file text.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
// Use readFileAsText() method to unlock a locked file from system
// or we can directly use a locked text of a file as parameter, to unlock.

var unlockedFileText = await bayunCore.unlockFileText(
  sessionId: "<sessionId>",
  lockedText: "<locked file text>",
);

writeFileTextToFile(unlockedFileText, fileInfoObject.metaData); // To write unlockedFileText to the file.
```

{% endtab %}
{% endtabs %}
