Skip to content

Instantly share code, notes, and snippets.

@bingomanatee
Last active September 28, 2022 20:26
Show Gist options
  • Save bingomanatee/d6a784219380d62fe93fb5b9039677e3 to your computer and use it in GitHub Desktop.
Save bingomanatee/d6a784219380d62fe93fb5b9039677e3 to your computer and use it in GitHub Desktop.
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { BASE64, HEX, UTF8 } from '../../constants';
import { RedisService } from '../../redis/redis.service';
import { encodedData, encodedInfo } from '../../types';
const algorithm = 'aes-192-cbc';
@Injectable()
export class CryptoService {
constructor(
@Inject(forwardRef(() => RedisService))
private redisService: RedisService,
) {}
private _cryptoModule;
get cryptoModule() {
if (!this._cryptoModule) {
this._cryptoModule = import('node:crypto');
}
return this._cryptoModule;
}
/**
*
* @param bucket {string}-- the name of the bucketConfig originating the message
* @param message {string} content
* @param initial {boolean} whether to get the key from the bucketConfig config or the session.
*/
async encode(id: string, message?: string): Promise<encodedData> {
if (!message) {
message = id;
}
const MASTER_KEY = process.env.MASTER_KEY;
const crypto = await this.cryptoModule;
// generate 16 bytes of random data known as vector
const seed = crypto.randomBytes(16);
const cipherKey = crypto.scryptSync(id, MASTER_KEY, 24);
const cipher = crypto.createCipheriv(algorithm, cipherKey, seed);
// encrypt the message
// input encoding
// output encoding
let encryptedData = cipher.update(message, UTF8, BASE64);
encryptedData = encryptedData + cipher.final(BASE64);
return {
id,
seed: Buffer.from(seed, HEX),
encryptedData,
};
}
async decode(id, message, seed) {
const seedBuffer = Buffer.from(seed, HEX);
const MASTER_KEY = process.env.MASTER_KEY;
const crypto = await this.cryptoModule;
const cipherKey = crypto.scryptSync(id, MASTER_KEY, 24);
const decipher = crypto.createDecipheriv(algorithm, cipherKey, seedBuffer);
try {
let decryptedData = decipher.update(message, BASE64, UTF8);
decryptedData += decipher.final(UTF8);
return {
id,
decryptedData,
};
} catch (err) {
console.log('error ', err.message, message);
throw err;
}
}
}
// use test
const id = uuid();
const token = await this.cryptoService.encode(id, tokenString);
const decoded = await this.cryptoService.decode(
id,
tokenString,
token.seed.toString(HEX),
);
console.log('decoded ', tokenString, 'as', decoded);
@bingomanatee
Copy link
Author

bingomanatee commented Sep 28, 2022

Trying to create a basic encode/decode libarary; getting odd resulsts

error  error:1C800064:Provider routines::bad decrypt XXXXXXX
error  error:1C80006B:Provider routines::wrong final block length XXXXXX
error  error:1C800064:Provider routines::bad decrypt XXXXXXX
error  error:1C800064:Provider routines::bad decrypt XXXXX
Error: error:1C800064:Provider routines::bad decrypt

odd thing, the "XXXX" are actually GOOD decryptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment