encryption-test
Advanced tools
Comparing version 1.0.7 to 1.0.8
23
index.js
@@ -380,3 +380,19 @@ 'use strict' | ||
} | ||
function getPrivateKeyByKeystore (keystoreContent, password) { | ||
let rawKeyResult = keystore.decrypt(keystoreContent, password) | ||
let privateKey = '' | ||
if (rawKeyResult.version === 2) { | ||
privateKey = rawKeyResult.privateKey | ||
} else { | ||
let chainType = keystoreContent.address.substring(8, 10); | ||
let rowPrivateKey = rawKeyResult.privateKey | ||
chainType = chainType === 'ef' ? CRYPTO_ED25519 : CRYPTO_SM2; | ||
privateKey = generatePrivateKeyBySeedAndCryptoType(chainType, rowPrivateKey) | ||
} | ||
const accountObj = generateByKey(privateKey) | ||
if (accountObj.address !== keystoreContent.address) { | ||
throw ('Keystore error'); | ||
} | ||
return privateKey | ||
} | ||
function _bytesToHex (bytes) { | ||
@@ -437,2 +453,5 @@ for (var hex = [], i = 0; i < bytes.length; i++) { | ||
} | ||
if (language === 'chinese') { | ||
word = WORDLISTS.chinese_simplified; | ||
} | ||
return bip39.entropyToMnemonic(entropy, word) | ||
@@ -597,2 +616,3 @@ } | ||
parsePublicKey, | ||
publicToAddress, | ||
isPrivateKey, | ||
@@ -603,2 +623,3 @@ isPublicKey, | ||
decipherKeyStore, | ||
getPrivateKeyByKeystore, | ||
generateChild, | ||
@@ -605,0 +626,0 @@ privKeyFromMCodeAndCrypto, |
{ | ||
"name": "encryption-test", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "", | ||
@@ -24,4 +24,5 @@ "main": "index.js", | ||
"json-bigint": "^1.0.0", | ||
"long": "^5.2.3" | ||
"long": "^5.2.3", | ||
"sha3": "^2.1.4" | ||
} | ||
} |
'use strict' | ||
const sjcl = require('brdc-sjcl') | ||
const CryptoJS = require('crypto-js') | ||
const {Keccak} = require('sha3'); | ||
const crypto = {} | ||
@@ -72,2 +73,50 @@ // const SCRYPT_PARAMS_COST_FACTOR = 16384 | ||
} | ||
crypto.decryptByV3 = (password, keystoreJson) => { | ||
const n = keystoreJson.crypto.kdfparams.n; | ||
const r = keystoreJson.crypto.kdfparams.r; | ||
const p = keystoreJson.crypto.kdfparams.p; | ||
const dkLen = keystoreJson.crypto.kdfparams.dklen * 8; | ||
let cypherText = {}; | ||
cypherText.ciphertext = CryptoJS.enc.Hex.parse(keystoreJson.crypto.ciphertext); | ||
let saltBits = sjcl.codec.hex.toBits(keystoreJson.crypto.kdfparams.salt); | ||
let passwordBits = sjcl.codec.utf8String.toBits(password); | ||
const dk = crypto.deriveKey(passwordBits, saltBits, keystoreJson.crypto.kdf, {n: n, r: r, p: p, dkLen: dkLen}); | ||
let macBody = sjcl.bitArray.concat(dk.slice(4, 8), cypherText.ciphertext.words); | ||
const derivedMac = keccak256(sjcl.codec.hex.fromBits(macBody)); | ||
if (derivedMac !== keystoreJson.crypto.mac) { | ||
throw ({code: '', codeKey: 'KeystorePasswordError', errorMessage: 'The password is incorrect'}); | ||
} | ||
const encryptKey = sjcl.codec.hex.fromBits(dk.slice(0, 4)); | ||
let aes = new sjcl.cipher.aes(sjcl.codec.hex.toBits(encryptKey)); | ||
return sjcl.mode.ctr.decrypt(aes, cypherText.ciphertext.words, sjcl.codec.hex.toBits(keystoreJson.crypto.cipherparams.iv)); | ||
} | ||
crypto.deriveKey = (password, saltBits, kdf, deriveConfig) => { | ||
if (kdf === 'scrypt') { | ||
return sjcl.misc.scrypt( | ||
password, | ||
saltBits, | ||
deriveConfig.n, | ||
deriveConfig.r, | ||
deriveConfig.p, | ||
deriveConfig.dkLen | ||
); | ||
} else if (kdf === 'pbkdf2') { | ||
// 使用pbkdf2进行密钥派生 | ||
return sjcl.misc.pbkdf2(password, sjcl.codec.hex.toBits(salt), 10000, 256, sjcl.misc.hmac); | ||
} else { | ||
throw new Error('Unsupported KDF algorithm'); | ||
} | ||
}; | ||
function keccak256(data) { | ||
const hash = new Keccak(256); | ||
hash.update(data, 'hex'); | ||
return hash.digest('hex'); | ||
} | ||
module.exports = crypto |
@@ -24,3 +24,17 @@ 'use strict' | ||
} | ||
keystore.decrypt = (keystoreContent, password) => { | ||
let version = keystoreContent.version || | ||
(keystoreContent.crypto || keystoreContent.Crypto) | ||
|| ''; | ||
if (!version) { | ||
throw new Error('Keystore error'); | ||
} | ||
let result = '' | ||
if (version === 2) { | ||
result = {version: version, privateKey: crypto.decrypt(password, keystoreContent)} | ||
} else { | ||
result = {version: version, privateKey: crypto.decryptByV3(password, keystoreContent)} | ||
} | ||
return result | ||
} | ||
module.exports = keystore |
75770
1346
11
+ Addedsha3@^2.1.4
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedsha3@2.1.4(transitive)