@waves/waves-crypto
Advanced tools
Comparing version 3.0.6 to 3.0.9
@@ -10,2 +10,3 @@ export declare const PUBLIC_KEY_LENGTH = 32; | ||
} | ||
export declare type AESMode = 'CBC' | 'CFB' | 'CTR' | 'OFB' | 'ECB'; | ||
export declare type TBytes = Uint8Array; | ||
@@ -12,0 +13,0 @@ export declare type TBase64 = string; |
27
index.js
@@ -31,2 +31,9 @@ "use strict"; | ||
const bytesToString = (bytes) => String.fromCharCode.apply(null, Array.from(_fromIn(bytes))); | ||
const aesModeMap = { | ||
'CBC': CryptoJS.mode.CBC, | ||
'CFB': CryptoJS.mode.CFB, | ||
'CTR': CryptoJS.mode.CTR, | ||
'OFB': CryptoJS.mode.OFB, | ||
'ECB': CryptoJS.mode.ECB, | ||
}; | ||
exports.crypto = (options) => { | ||
@@ -37,3 +44,3 @@ if (options && options.seed == '') | ||
const c2 = (f) => (a) => (b) => f(a, b); | ||
const c3 = (f) => (a) => (b) => (c) => f(a, b, c); | ||
const c3 = (f) => (a) => (b, c) => f(a, b, c); | ||
const isWords = (val) => val.words !== undefined || | ||
@@ -167,10 +174,10 @@ val.key !== undefined; | ||
const seed = (seed, nonce) => ({ seed: exports.Seed.toBinary(seed).seed, nonce }); | ||
const aesEncrypt = (data, secret, iv, mode = 'ECB') => _toOut(base64Decode(CryptoJS.AES.encrypt(_toWords(_fromIn(data)), _toWords(_fromIn(secret)), { | ||
const aesEncrypt = (data, secret, mode = 'CBC', iv) => _toOut(base64Decode(CryptoJS.AES.encrypt(_toWords(_fromIn(data)), _toWords(_fromIn(secret)), { | ||
iv: iv ? _toWords(_fromIn(iv)) : undefined, | ||
mode: mode === 'ECB' ? CryptoJS.mode.ECB : CryptoJS.mode.CTR, | ||
mode: aesModeMap[mode], | ||
}) | ||
.toString())); | ||
const aesDecrypt = (encryptedMessage, secret, iv, mode = 'ECB') => _toOut(_fromWords(CryptoJS.AES.decrypt(base64Encode(encryptedMessage), _toWords(_fromIn(secret)), { | ||
const aesDecrypt = (encryptedMessage, secret, mode = 'CBC', iv) => _toOut(_fromWords(CryptoJS.AES.decrypt(base64Encode(encryptedMessage), _toWords(_fromIn(secret)), { | ||
iv: iv ? _toWords(_fromIn(iv)) : undefined, | ||
mode: mode === 'ECB' ? CryptoJS.mode.ECB : CryptoJS.mode.CTR, | ||
mode: aesModeMap[mode], | ||
}))); | ||
@@ -187,4 +194,4 @@ const hmacSHA256 = (message, key) => _toOut(_fromWords(CryptoJS.HmacSHA256(_toWords(_fromIn(message)), _toWords(_fromIn(key))))); | ||
const m = _fromRawIn(message); | ||
const Cc = aesEncrypt(m, CEK, IV, 'CTR'); | ||
const Ccek = aesEncrypt(CEK, sharedKey); | ||
const Cc = aesEncrypt(m, CEK, 'CTR', IV); | ||
const Ccek = aesEncrypt(CEK, sharedKey, 'ECB'); | ||
const Mhmac = hmacSHA256(m, CEK); | ||
@@ -197,8 +204,8 @@ const CEKhmac = hmacSHA256(concat(CEK, IV), sharedKey); | ||
const [Ccek, _CEKhmac, Cc, _Mhmac, iv,] = split(encryptedMessage, 48, 32, 32, 32, 16); | ||
const CEK = _fromIn(aesDecrypt(Ccek, sharedKey)); | ||
const CEK = _fromIn(aesDecrypt(Ccek, sharedKey, 'ECB')); | ||
const CEKhmac = _fromIn(hmacSHA256(concat(CEK, iv), _fromIn(sharedKey))); | ||
const isValidKey = CEKhmac.every((v, i) => v === _CEKhmac[i]); | ||
if (!isValidKey) | ||
throw new Error('Invalid message'); | ||
const M = _fromIn(aesDecrypt(Cc, CEK, iv, 'CTR')); | ||
throw new Error('Invalid key'); | ||
const M = _fromIn(aesDecrypt(Cc, CEK, 'CTR', iv)); | ||
const Mhmac = _fromIn(hmacSHA256(M, CEK)); | ||
@@ -205,0 +212,0 @@ const isValidMessage = Mhmac.every((v, i) => v === _Mhmac[i]); |
{ | ||
"name": "@waves/waves-crypto", | ||
"version": "3.0.6", | ||
"version": "3.0.9", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
Sorry, the diff of this file is not supported yet
139482
2804