chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
61 lines • 1.9 kB
JavaScript
import { randomBytes } from '@noble/hashes/utils';
import { gcm } from '@noble/ciphers/aes';
import { pbkdf2 } from '../../../InternalUtils/Crypto';
const iterations = 600_000;
const dkLen = 32;
export class IncorrectPassword extends Error {
constructor() {
super('Password is incorrect');
if (Error.captureStackTrace)
Error.captureStackTrace(this, IncorrectPassword);
this.name = this.constructor.name;
}
}
export class Encrypted {
iterations;
dkLen;
nonce;
salt;
data;
cipher;
constructor(params) {
this.iterations = params.iterations;
this.dkLen = params.dkLen;
this.nonce = params.nonce;
this.salt = params.salt;
this.data = params.data;
this.cipher = params.cipher;
}
static async encrypt(dataToEncrypt, password) {
const cipher = 'aes-gcm';
const salt = randomBytes(32);
const nonce = randomBytes(12);
const derivedKey = await pbkdf2({ iterations, dkLen, salt, password });
const data = gcm(derivedKey, nonce).encrypt(dataToEncrypt);
return new Encrypted({
iterations,
dkLen,
nonce,
salt,
data,
cipher,
});
}
static async decrypt(encrypted, password) {
const iterations = encrypted.iterations;
const dkLen = encrypted.dkLen;
const salt = encrypted.salt;
const nonce = encrypted.nonce;
const derivedKey = await pbkdf2({ iterations, dkLen, salt, password });
try {
return gcm(derivedKey, nonce).decrypt(encrypted.data);
}
catch (ex) {
if ('message' in ex && ex.message == 'aes/gcm: invalid ghash tag')
throw new IncorrectPassword();
else
throw ex;
}
}
}
//# sourceMappingURL=Encrypted.js.map