hesabe-crypt
Version:
Encryption library for Hesabe Payment API 2.0
62 lines (52 loc) • 1.44 kB
JavaScript
const aesjs = require("aes-js");
class HesabeCrypt {
constructor(secret, iv) {
this.key = secret;
this.iv = iv;
}
encryptAes(txt) {
let paddedTxt = this.pkcs5Pad(txt);
let txtBytes = aesjs.utils.utf8.toBytes(paddedTxt);
let aesCbc = new aesjs.ModeOfOperation.cbc(this.key, this.iv);
let encBytes = aesCbc.encrypt(txtBytes);
let encHex = aesjs.utils.hex.fromBytes(encBytes);
return encHex;
}
decryptAes(encHex) {
let encBytes = aesjs.utils.hex.toBytes(encHex);
let aesCbc = new aesjs.ModeOfOperation.cbc(this.key, this.iv);
let decBytes = aesCbc.decrypt(encBytes);
let decTxt = aesjs.utils.utf8.fromBytes(decBytes);
let strippedTxt = this.pkcs5Strip(decTxt);
return strippedTxt;
}
pkcs5Pad(txt) {
let blockSize = 32;
let padLen = blockSize - (txt.length % blockSize);
return txt + this.strRepeat(String.fromCharCode(padLen), padLen);
}
pkcs5Strip(txt) {
let padChar = txt.charAt(txt.length - 1);
let i = txt.indexOf(padChar);
let strippedTxt = txt.substr(0, i);
return strippedTxt;
}
strRepeat(input, multiplier) {
// eslint-disable-line camelcase
let y = "";
while (true) {
if (multiplier & 1) {
y += input;
}
multiplier >>= 1;
if (multiplier) {
input += input;
} else {
break;
}
}
return y;
}
}
module.exports = HesabeCrypt;
;