@harmony-js/crypto
Advanced tools
Comparing version 0.0.32 to 0.0.33
import { Arrayish } from './bytes'; | ||
export declare function keccak256(data: Arrayish): string; | ||
export declare function sha3_256(data: Arrayish): string; | ||
//# sourceMappingURL=keccak256.d.ts.map |
@@ -15,2 +15,10 @@ "use strict"; | ||
exports.keccak256 = keccak256; | ||
function sha3_256(data) { | ||
var arrayified = bytes_1.arrayify(data); | ||
if (arrayified) { | ||
return '0x' + sha3.sha3_256(arrayified); | ||
} | ||
throw new Error('arrayify failed'); | ||
} | ||
exports.sha3_256 = sha3_256; | ||
//# sourceMappingURL=keccak256.js.map |
@@ -21,2 +21,4 @@ import { EncryptOptions, Keystore } from './types'; | ||
export declare const decrypt: (keystore: Keystore, password: string) => Promise<string>; | ||
export declare const encryptPhrase: (phrase: string, password: string, options?: EncryptOptions | undefined) => Promise<string>; | ||
export declare const decryptPhrase: (keystore: Keystore, password: string) => Promise<string>; | ||
//# sourceMappingURL=keystore.d.ts.map |
@@ -135,2 +135,60 @@ "use strict"; | ||
}); }; | ||
exports.encryptPhrase = function (phrase, password, options) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var salt, iv, kdf, level, uuidRandom, n, kdfparams, derivedKey, cipher, ciphertext, mac; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!password) { | ||
throw new Error('password is not found'); | ||
} | ||
salt = random_1.randomBytes(32); | ||
iv = Buffer.from(random_1.randomBytes(16), 'hex'); | ||
kdf = options !== undefined ? (options.kdf ? options.kdf : 'scrypt') : 'scrypt'; | ||
level = options !== undefined ? (options.level ? options.level : 8192) : 8192; | ||
uuidRandom = options !== undefined ? options.uuid : undefined; | ||
n = kdf === 'pbkdf2' ? 262144 : level; | ||
kdfparams = { | ||
salt: salt, | ||
n: n, | ||
r: 8, | ||
p: 1, | ||
dklen: 32, | ||
}; | ||
return [4 /*yield*/, getDerivedKey(Buffer.from(password), kdf, kdfparams)]; | ||
case 1: | ||
derivedKey = _a.sent(); | ||
cipher = new aes_js_1.default.ModeOfOperation.ctr(derivedKey.slice(0, 16), new aes_js_1.default.Counter(iv)); | ||
if (!cipher) { | ||
throw new Error('Unsupported cipher'); | ||
} | ||
ciphertext = Buffer.from(cipher.encrypt(Buffer.from(phrase))); | ||
mac = keccak256_1.keccak256(bytes_1.concat([derivedKey.slice(16, 32), ciphertext])); | ||
return [2 /*return*/, JSON.stringify({ | ||
version: 3, | ||
id: uuid_1.default.v4({ random: uuidRandom || bytes_1.hexToIntArray(random_1.randomBytes(16)) }), | ||
Crypto: { | ||
ciphertext: ciphertext.toString('hex'), | ||
cipherparams: { | ||
iv: iv.toString('hex'), | ||
}, | ||
cipher: DEFAULT_ALGORITHM, | ||
kdf: kdf, | ||
kdfparams: kdfparams, | ||
mac: mac.replace('0x', ''), | ||
}, | ||
})]; | ||
} | ||
}); | ||
}); }; | ||
exports.decryptPhrase = function (keystore, password) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var result; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, exports.decrypt(keystore, password)]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, Buffer.from(result.replace('0x', ''), 'hex').toString()]; | ||
} | ||
}); | ||
}); }; | ||
//# sourceMappingURL=keystore.js.map |
@@ -21,3 +21,3 @@ export declare type KDF = 'pbkdf2' | 'scrypt'; | ||
export interface Keystore { | ||
address: string; | ||
address?: string; | ||
Crypto: { | ||
@@ -24,0 +24,0 @@ cipher: string; |
{ | ||
"name": "@harmony-js/crypto", | ||
"version": "0.0.32", | ||
"version": "0.0.33", | ||
"description": "crypto libraries for harmony", | ||
@@ -21,3 +21,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@harmony-js/utils": "0.0.32", | ||
"@harmony-js/utils": "0.0.33", | ||
"aes-js": "^3.1.2", | ||
@@ -34,3 +34,3 @@ "bip39": "^2.5.0", | ||
}, | ||
"gitHead": "ac90d10deaf715c4254bf7691e752332d0657d52" | ||
"gitHead": "6336e94dfe5e478c5109400659537330af188e23" | ||
} |
@@ -13,1 +13,9 @@ // this file is ported from 'ether.js' and done some fixes | ||
} | ||
export function sha3_256(data: Arrayish): string { | ||
const arrayified = arrayify(data); | ||
if (arrayified) { | ||
return '0x' + sha3.sha3_256(arrayified); | ||
} | ||
throw new Error('arrayify failed'); | ||
} |
@@ -166,1 +166,63 @@ import aes from 'aes-js'; | ||
}; | ||
export const encryptPhrase = async ( | ||
phrase: string, | ||
password: string, | ||
options?: EncryptOptions, | ||
): Promise<string> => { | ||
if (!password) { | ||
throw new Error('password is not found'); | ||
} | ||
const salt = randomBytes(32); | ||
const iv = Buffer.from(randomBytes(16), 'hex'); | ||
const kdf = | ||
options !== undefined ? (options.kdf ? options.kdf : 'scrypt') : 'scrypt'; | ||
const level = | ||
options !== undefined ? (options.level ? options.level : 8192) : 8192; | ||
const uuidRandom = options !== undefined ? options.uuid : undefined; | ||
const n = kdf === 'pbkdf2' ? 262144 : level; | ||
const kdfparams = { | ||
salt, | ||
n, | ||
r: 8, | ||
p: 1, | ||
dklen: 32, | ||
}; | ||
const derivedKey = await getDerivedKey(Buffer.from(password), kdf, kdfparams); | ||
const cipher = new aes.ModeOfOperation.ctr( | ||
derivedKey.slice(0, 16), | ||
new aes.Counter(iv), | ||
); | ||
if (!cipher) { | ||
throw new Error('Unsupported cipher'); | ||
} | ||
const ciphertext = Buffer.from(cipher.encrypt(Buffer.from(phrase))); | ||
const mac = keccak256(concat([derivedKey.slice(16, 32), ciphertext])); | ||
return JSON.stringify({ | ||
version: 3, | ||
id: uuid.v4({ random: uuidRandom || hexToIntArray(randomBytes(16)) }), | ||
Crypto: { | ||
ciphertext: ciphertext.toString('hex'), | ||
cipherparams: { | ||
iv: iv.toString('hex'), | ||
}, | ||
cipher: DEFAULT_ALGORITHM, | ||
kdf, | ||
kdfparams, | ||
mac: mac.replace('0x', ''), | ||
}, | ||
}); | ||
}; | ||
export const decryptPhrase = async ( | ||
keystore: Keystore, | ||
password: string, | ||
): Promise<string> => { | ||
const result = await decrypt(keystore, password); | ||
return Buffer.from(result.replace('0x', ''), 'hex').toString(); | ||
}; |
@@ -26,3 +26,3 @@ export type KDF = 'pbkdf2' | 'scrypt'; | ||
export interface Keystore { | ||
address: string; | ||
address?: string; | ||
Crypto: { | ||
@@ -29,0 +29,0 @@ cipher: string; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
585303
7189
+ Added@harmony-js/utils@0.0.33(transitive)
- Removed@harmony-js/utils@0.0.32(transitive)
Updated@harmony-js/utils@0.0.33