6.2 Lock/Unlock File Text
6.2.1 Read a file as text
The locked/unlocked file can be read as text as shown in the code snippet.
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
}
};
}
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.
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();
}
6.2.3 Lock File Text
The lockFileText
function locks a file data as text with default BayunEncryptionPolicy and BayunKeyGenerationPolicy 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 to read file as file text.
readFileAsText();
var lockedFileText = await bayunCore.lockFileText("<sessionId>", fileInfoObject.fileText);
writeFileTextToFile(lockedFileText, fileInfoObject.metaData); // To write lockedFileText to the file.
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 to read file as file text.
encryptionPolicy : BayunEncryptionPolicy determines the key to be used to generate the lockingKey.
keyGenerationPolicy : BayunKeyGenerationPolicy determines the policy to generate the lockingKey.
groupId : GroupId is required if encryptionPolicy is
GROUP
.
const encryptionPolicy = BayunCore.EncryptionPolicy.GROUP;
const keyGenerationPolicy = BayunCore.KeyGenerationPolicy.ENVELOPE;
readFileAsText();
var lockedFileText = await bayunCore.lockFileText("<sessionId>", fileInfoObject.fileText,
encryptionPolicy, keyGenerationPolicy, "<groupId>");
writeFileTextToFile(lockedFileText, fileInfoObject.metaData); // To write lockedFileText to the file.
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 to read locked file as file text.
// 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>", "<locked file text>");
writeFileTextToFile(unlockedFileText, fileInfoObject.metaData); // To write unlockedFileText to the file.
Last updated
Was this helpful?