soda-sdk
Version:
This SDK provides functionalities for AES and RSA encryption schemes, ECDSA signature scheme and some functionalities used for working with sodalabs blockchain.
33 lines (24 loc) • 964 B
text/typescript
import {BLOCK_SIZE} from "./crypto"
import {ethers} from "ethers";
import fs from "fs";
export function loadAesKey(filePath: string): Buffer {
// Read the hex-encoded contents of the file
const hexKey = fs.readFileSync(filePath, 'utf8').trim();
// Decode the hex string to binary
const key = Buffer.from(hexKey.slice(2), 'hex');
// Ensure the key is the correct length
if (key.length !== BLOCK_SIZE) {
throw new RangeError(`Invalid key length: ${key.length} bytes, must be 16 bytes`);
}
return key;
}
export function writeAesKey(filePath:string, key: Buffer): void {
// Ensure the key is the correct length
if (key.length !== BLOCK_SIZE) {
throw new RangeError(`Invalid key length: ${key.length} bytes, must be 16 bytes`);
}
// Encode the key to hex string
const hexKey = ethers.hexlify(key);
// Write the hex-encoded key to the file
fs.writeFileSync(filePath, hexKey, 'utf8');
}