2-Single User with UnStructured Data

In this scenario, users need to store pictures in an encrypted format to protect their privacy and ensure the security of their data. The system uses the Mind Lake library to achieve this by encrypting the pictures before saving them and decrypting them upon retrieval.

2.1. Task

  1. Encrypting the picture data and uploading to MindLake.

  2. Querying and decrypting the encrypted picture data from MindLake.

2.2. Example code to Insert cipher

import {MindLake} from 'mind-lake-sdk';
import md5 from 'js-md5';
const DataType = MindLake.DataType;
  
const response = await fetch("https://avatars.githubusercontent.com/u/97393721");
if(!response || response.status !== 200) {
  return console.error("Failed to get picture from github")
}
const image = await response.arrayBuffer();
console.log("MD5 of the original picture pic_origin.png: ", md5(image));
const base64 = Buffer.from(image).toString('base64');
const mindLake = await MindLake.getInstance("YOU OWN APP KEY");

// connect to MindLake."5" is example of Goerli Testnet chainId
const chainId = "5"
const res1 = await mindLake.connect(chainId);
if(res1.code !== 0) {
  console.error(res1.message);
  return
}

// create a table
const dataLake = mindLake.dataLake;
await dataLake.dropTable("album");
const res2 = await dataLake.createTable("album", [{columnName: 'name', type: DataType.text, encrypt: false}, {columnName: 'picture', type: DataType.text, encrypt: true}]);
if(res2.code !==0) {
  console.error(res2.message);
  return
}
const cryptor = mindLake.cryptor;
const res3 = await cryptor.encrypt(base64, "album.picture");
if(res3.code !== 0) {
  console.error(res3.message);
  return
}
const sql = `insert into album (name, picture) values ("mind.png", '${res3.result}')`;
const res4 = await dataLake.query(sql);
if(res4.code !== 0) {
  console.error(res4.message);
  return
}

2.3. Result Table in MindLake

namepicture

(text, encrypt=False)

(text, encrypt=True)

mind.png

0x11223344

2.4. Example code to query on cipher

const selectSql = `select * from album`;
const res9 = await dataLake.query(selectSql);
if(res9.code !== 0) {
  console.error(res9.message);
  return
}
for (const row of res9.result.data) {
  const decrypt = await cryptor.decrypt(row[1]);
  if(decrypt.code !== 0) {
    return console.error(decrypt.message)
  }
  console.log(`MD5 of the decrypted picture ${row[0]}: `, md5(Buffer.from(decrypt.result, 'hex')))
}

2.5. Output

MD5 of the original picture pic_origin.png: 7b9b1b4591f6af62bda68dd97a2728ca
MD5 of the decrypted picture mind.png: 7b9b1b4591f6af62bda68dd97a2728ca

Last updated