I’m extremely new to encryption and I’ve been following tutorials online. I’m trying to encrypt a file using crypto module of Node.js before uploading it to IPFS. Here’s my code:
const encrypt = (data) => {
const crypto = require('crypto');
//compute hash and iv
const algorithm = 'aes-256-ctr';
const iv = crypto.randomBytes(16);
key = crypto.createHash('sha256').update(data).digest('base64').substring(0,32);
const cipher = crypto.createCipheriv(algorithm, key, iv);
const result = iv + cipher.update(data) + cipher.final('hex');
return result;
}
uploadFile = async() => {
//encrypt file
const data = credential.files[0];
const encryptedFile = encrypt(data);
//upload encrypted file in IPFS
const file = new Moralis.File(data.name, encryptedFile);
await file.saveIPFS();
console.log(file.ipfs(), file.hash());
return file.ipfs();
}
I got this error
TypeError: Cannot create a Parse.File with that data.
I know I’m not getting the right type of data in EncryptedFile but I have no idea have to fix it. I tried displaying EncryptedFile in console and it only shows the IV, but cipher.update(data) and cipher.final() is not returning anything.