Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window = {};
- // (new Function(pm.globals.get('dayjs')))();
- eval(pm.globals.get('nodeforge'))
- function init(){
- let libs = {}
- libs.getSecret = function(access_token){
- return new Promise((resolve, reject) => {
- pm.sendRequest({
- url: `${pm.environment.get('baseUrl')}/auth/v1/secret`,
- method: 'GET',
- header: {
- 'Content-Type': 'application/json',
- 'Authorization': `Bearer ${access_token}`
- }
- }, (err, res) => {
- if (err) {
- return reject(err);
- } else {
- return resolve(res);
- }
- });
- });
- }
- libs.pef1Encrypt = function (plainText, secret) {
- // let iv = crypto.randomBytes(12);
- let iv = forge.random.getBytesSync(12);
- // let salt = crypto.randomBytes(16);
- let salt = forge.random.getBytesSync(16);
- // let key = crypto.createHmac('sha256', secret).update(salt).digest();
- const hmac = forge.hmac.create();
- hmac.start('sha256', secret);
- hmac.update(salt);
- let key = hmac.digest();
- // let cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
- let cipher = forge.cipher.createCipher('AES-GCM', key);
- cipher.start({
- iv: iv, // should be a 12-byte binary-encoded string or byte buffer
- tagLength: 128 // optional, defaults to 128 bits
- });
- // let cipherText = cipher.update(plainText, 'utf8', 'base64') + cipher.final('base64');
- cipher.update(forge.util.createBuffer(plainText,'utf8'));
- cipher.finish();
- let cipherText = forge.util.encode64(cipher.output.getBytes()) ;
- // let tag = cipher.getAuthTag();
- let tag = cipher.mode.tag;
- let msg = {
- f: 'pef1',
- s: forge.util.encode64(salt),
- i: forge.util.encode64(iv),
- t: forge.util.encode64(tag.getBytes()),
- d: cipherText
- };
- return JSON.stringify(msg);
- }
- libs.pef1Decrypt = function(encryptedText, secret) {
- try {
- let msg = JSON.parse(encryptedText);
- let salt = forge.util.decode64(msg.s, 'base64');
- let iv = forge.util.decode64(msg.i, 'base64');
- let tag = new forge.util.ByteStringBuffer(forge.util.decode64(msg.t, 'base64'));
- let cipherText = forge.util.decode64(msg.d);
- // let key = crypto.createHmac('sha256', secret).update(salt).digest();
- const hmac = forge.hmac.create();
- hmac.start('sha256', secret);
- hmac.update(salt);
- let key = hmac.digest();
- // let decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
- // decipher.setAuthTag(tag);
- let decipher = forge.cipher.createDecipher('AES-GCM', key);
- decipher.start({
- iv: iv,
- // additionalData: 'binary-encoded string', // optional
- tagLength: 128, // optional, defaults to 128 bits
- tag: tag // authentication tag from encryption
- });
- decipher.update(new forge.util.ByteStringBuffer(cipherText));
- decipher.finish()
- return forge.util.encodeUtf8(decipher.output.getBytes())
- // return decipher.update(cipherText, 'base64', 'utf8') + decipher.final('utf8');
- } catch (e) {
- // will return null if cipherText is not in pef1 format or authentication tag validation fails
- return null;
- }
- }
- return libs
- }
- init();
Add Comment
Please, Sign In to add comment