@liskhq/lisk-cryptography
Advanced tools
Comparing version 4.0.0-beta.2 to 4.0.0-beta.3
@@ -7,3 +7,2 @@ "use strict"; | ||
const utils_1 = require("./utils"); | ||
const CHARSET = 'zxvcpmbn3465o978uyrtkqew2adsjhfg'; | ||
const convertUIntArray = (uintArray, fromBits, toBits) => { | ||
@@ -28,3 +27,3 @@ const maxValue = (1 << toBits) - 1; | ||
}; | ||
const convertUInt5ToBase32 = (uint5Array) => uint5Array.map((val) => CHARSET[val]).join(''); | ||
const convertUInt5ToBase32 = (uint5Array) => uint5Array.map((val) => constants_1.LISK32_CHARSET[val]).join(''); | ||
const getAddressFromPublicKey = (publicKey) => { | ||
@@ -34,3 +33,3 @@ const buffer = (0, utils_1.hash)(publicKey); | ||
if (truncatedBuffer.length !== constants_1.BINARY_ADDRESS_LENGTH) { | ||
throw new Error('The Lisk addresses must contains exactly 20 bytes'); | ||
throw new Error(`Lisk address must contain exactly ${constants_1.BINARY_ADDRESS_LENGTH} bytes`); | ||
} | ||
@@ -80,7 +79,5 @@ return truncatedBuffer; | ||
exports.getLisk32AddressFromPublicKey = getLisk32AddressFromPublicKey; | ||
const LISK32_ADDRESS_LENGTH = 41; | ||
const LISK32_CHARSET = 'zxvcpmbn3465o978uyrtkqew2adsjhfg'; | ||
const validateLisk32Address = (address, prefix = constants_1.DEFAULT_LISK32_ADDRESS_PREFIX) => { | ||
if (address.length !== LISK32_ADDRESS_LENGTH) { | ||
throw new Error('Address length does not match requirements. Expected 41 characters.'); | ||
if (address.length !== constants_1.LISK32_ADDRESS_LENGTH) { | ||
throw new Error(`Address length does not match requirements. Expected ${constants_1.LISK32_ADDRESS_LENGTH} characters.`); | ||
} | ||
@@ -92,6 +89,6 @@ const addressPrefix = address.substring(0, 3); | ||
const addressSubstringArray = address.substring(3).split(''); | ||
if (!addressSubstringArray.every(char => LISK32_CHARSET.includes(char))) { | ||
throw new Error("Invalid character found in address. Only allow characters: 'abcdefghjkmnopqrstuvwxyz23456789'."); | ||
if (!addressSubstringArray.every(char => constants_1.LISK32_CHARSET.includes(char))) { | ||
throw new Error(`Invalid character found in address. Only allow characters: '${constants_1.LISK32_CHARSET}'.`); | ||
} | ||
const integerSequence = addressSubstringArray.map(char => LISK32_CHARSET.indexOf(char)); | ||
const integerSequence = addressSubstringArray.map(char => constants_1.LISK32_CHARSET.indexOf(char)); | ||
if (!verifyChecksum(integerSequence)) { | ||
@@ -107,3 +104,3 @@ throw new Error('Invalid checksum for address.'); | ||
const addressArray = base32AddressNoPrefixNoChecksum.split(''); | ||
const integerSequence = addressArray.map(char => LISK32_CHARSET.indexOf(char)); | ||
const integerSequence = addressArray.map(char => constants_1.LISK32_CHARSET.indexOf(char)); | ||
const integerSequence8 = convertUIntArray(integerSequence, 5, 8); | ||
@@ -110,0 +107,0 @@ return Buffer.from(integerSequence8); |
@@ -5,3 +5,2 @@ /// <reference types="node" /> | ||
export declare const DEFAULT_LISK32_ADDRESS_PREFIX = "lsk"; | ||
export declare const DEFAULT_BASE32_ADDRESS_PREFIX = "lsk"; | ||
export declare const ED25519_CURVE = "ed25519 seed"; | ||
@@ -14,1 +13,3 @@ export declare const MAX_UINT32 = 4294967295; | ||
export declare const SHA256 = "sha256"; | ||
export declare const LISK32_CHARSET = "zxvcpmbn3465o978uyrtkqew2adsjhfg"; | ||
export declare const LISK32_ADDRESS_LENGTH = 41; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SHA256 = exports.EMPTY_SALT = exports.L = exports.HASH_LENGTH = exports.HARDENED_OFFSET = exports.MAX_UINT32 = exports.ED25519_CURVE = exports.DEFAULT_BASE32_ADDRESS_PREFIX = exports.DEFAULT_LISK32_ADDRESS_PREFIX = exports.BINARY_ADDRESS_LENGTH = exports.SIGNED_MESSAGE_PREFIX = void 0; | ||
exports.LISK32_ADDRESS_LENGTH = exports.LISK32_CHARSET = exports.SHA256 = exports.EMPTY_SALT = exports.L = exports.HASH_LENGTH = exports.HARDENED_OFFSET = exports.MAX_UINT32 = exports.ED25519_CURVE = exports.DEFAULT_LISK32_ADDRESS_PREFIX = exports.BINARY_ADDRESS_LENGTH = exports.SIGNED_MESSAGE_PREFIX = void 0; | ||
exports.SIGNED_MESSAGE_PREFIX = 'Lisk Signed Message:\n'; | ||
exports.BINARY_ADDRESS_LENGTH = 20; | ||
exports.DEFAULT_LISK32_ADDRESS_PREFIX = 'lsk'; | ||
exports.DEFAULT_BASE32_ADDRESS_PREFIX = exports.DEFAULT_LISK32_ADDRESS_PREFIX; | ||
exports.ED25519_CURVE = 'ed25519 seed'; | ||
@@ -15,2 +14,4 @@ exports.MAX_UINT32 = 4294967295; | ||
exports.SHA256 = 'sha256'; | ||
exports.LISK32_CHARSET = 'zxvcpmbn3465o978uyrtkqew2adsjhfg'; | ||
exports.LISK32_ADDRESS_LENGTH = 41; | ||
//# sourceMappingURL=constants.js.map |
/// <reference types="node" /> | ||
export declare const ARGON2_MEMORY = 2097023; | ||
export declare enum Cipher { | ||
AES256GCM = "aes-256-gcm" | ||
AES128GCM = "aes-128-gcm" | ||
} | ||
@@ -27,3 +27,3 @@ export declare enum KDF { | ||
} | ||
export declare const encryptAES256GCMWithPassword: (plainText: string | Buffer, password: string, options?: { | ||
export declare const encryptAES128GCMWithPassword: (plainText: string | Buffer, password: string, options?: { | ||
kdf?: KDF; | ||
@@ -36,4 +36,4 @@ kdfparams?: { | ||
}) => Promise<EncryptedMessageObject>; | ||
export declare function decryptAES256GCMWithPassword(encryptedMessage: EncryptedMessageObject, password: string): Promise<Buffer>; | ||
export declare function decryptAES256GCMWithPassword(encryptedMessage: EncryptedMessageObject, password: string, encoding: 'utf8' | 'utf-8'): Promise<string>; | ||
export declare function decryptAES128GCMWithPassword(encryptedMessage: EncryptedMessageObject, password: string): Promise<Buffer>; | ||
export declare function decryptAES128GCMWithPassword(encryptedMessage: EncryptedMessageObject, password: string, encoding: 'utf8' | 'utf-8'): Promise<string>; | ||
export declare const encryptMessageWithPassword: (plainText: string | Buffer, password: string, options?: { | ||
@@ -47,4 +47,4 @@ kdf?: KDF; | ||
}) => Promise<EncryptedMessageObject>; | ||
export declare const decryptMessageWithPassword: typeof decryptAES256GCMWithPassword; | ||
export declare const decryptMessageWithPassword: typeof decryptAES128GCMWithPassword; | ||
export declare const parseEncryptedMessage: (encryptedMessage: string) => EncryptedMessageObject; | ||
export declare const stringifyEncryptedMessage: (encryptedMessage: EncryptedMessageObject) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stringifyEncryptedMessage = exports.parseEncryptedMessage = exports.decryptMessageWithPassword = exports.encryptMessageWithPassword = exports.decryptAES256GCMWithPassword = exports.encryptAES256GCMWithPassword = exports.KDF = exports.Cipher = exports.ARGON2_MEMORY = void 0; | ||
exports.stringifyEncryptedMessage = exports.parseEncryptedMessage = exports.decryptMessageWithPassword = exports.encryptMessageWithPassword = exports.decryptAES128GCMWithPassword = exports.encryptAES128GCMWithPassword = exports.KDF = exports.Cipher = exports.ARGON2_MEMORY = void 0; | ||
const hash_wasm_1 = require("hash-wasm"); | ||
@@ -28,3 +28,3 @@ const querystring = require("querystring"); | ||
(function (Cipher) { | ||
Cipher["AES256GCM"] = "aes-256-gcm"; | ||
Cipher["AES128GCM"] = "aes-128-gcm"; | ||
})(Cipher = exports.Cipher || (exports.Cipher = {})); | ||
@@ -36,7 +36,7 @@ var KDF; | ||
})(KDF = exports.KDF || (exports.KDF = {})); | ||
const encryptAES256GCMWithPassword = async (plainText, password, options) => { | ||
const encryptAES128GCMWithPassword = async (plainText, password, options) => { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
const kdf = (_a = options === null || options === void 0 ? void 0 : options.kdf) !== null && _a !== void 0 ? _a : KDF.ARGON2; | ||
const IV_BUFFER_SIZE = 12; | ||
const SALT_BUFFER_SIZE = 16; | ||
const IV_BUFFER_SIZE = 16; | ||
const SALT_BUFFER_SIZE = 8; | ||
const salt = crypto.randomBytes(SALT_BUFFER_SIZE); | ||
@@ -56,3 +56,3 @@ const iv = crypto.randomBytes(IV_BUFFER_SIZE); | ||
: getKeyFromPassword(password, salt, iterations); | ||
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv); | ||
const cipher = crypto.createCipheriv('aes-128-gcm', key.slice(0, 16), iv); | ||
const firstBlock = Buffer.isBuffer(plainText) | ||
@@ -73,3 +73,3 @@ ? cipher.update(plainText) | ||
}, | ||
cipher: Cipher.AES256GCM, | ||
cipher: Cipher.AES128GCM, | ||
cipherparams: { | ||
@@ -82,3 +82,3 @@ iv: iv.toString('hex'), | ||
}; | ||
exports.encryptAES256GCMWithPassword = encryptAES256GCMWithPassword; | ||
exports.encryptAES128GCMWithPassword = encryptAES128GCMWithPassword; | ||
const getTagBuffer = (tag) => { | ||
@@ -92,3 +92,3 @@ const TAG_BUFFER_SIZE = 16; | ||
}; | ||
async function decryptAES256GCMWithPassword(encryptedMessage, password, encoding) { | ||
async function decryptAES128GCMWithPassword(encryptedMessage, password, encoding) { | ||
const { kdf, ciphertext, cipherparams: { iv, tag }, kdfparams: { parallelism, salt, iterations, memorySize }, } = encryptedMessage; | ||
@@ -105,3 +105,3 @@ const tagBuffer = getTagBuffer(tag); | ||
: getKeyFromPassword(password, (0, utils_1.hexToBuffer)(salt, 'Salt'), iterations); | ||
const decipher = crypto.createDecipheriv('aes-256-gcm', key, (0, utils_1.hexToBuffer)(iv, 'IV')); | ||
const decipher = crypto.createDecipheriv('aes-128-gcm', key.slice(0, 16), (0, utils_1.hexToBuffer)(iv, 'IV')); | ||
decipher.setAuthTag(tagBuffer); | ||
@@ -115,5 +115,5 @@ const firstBlock = decipher.update((0, utils_1.hexToBuffer)(ciphertext, 'Cipher text')); | ||
} | ||
exports.decryptAES256GCMWithPassword = decryptAES256GCMWithPassword; | ||
exports.encryptMessageWithPassword = exports.encryptAES256GCMWithPassword; | ||
exports.decryptMessageWithPassword = decryptAES256GCMWithPassword; | ||
exports.decryptAES128GCMWithPassword = decryptAES128GCMWithPassword; | ||
exports.encryptMessageWithPassword = exports.encryptAES128GCMWithPassword; | ||
exports.decryptMessageWithPassword = decryptAES128GCMWithPassword; | ||
const parseOption = (optionString) => { | ||
@@ -150,3 +150,3 @@ const option = !optionString ? undefined : parseInt(optionString, 10); | ||
} | ||
const cipherTypes = [Cipher.AES256GCM]; | ||
const cipherTypes = [Cipher.AES128GCM]; | ||
if (!cipherTypes.includes(cipher)) { | ||
@@ -153,0 +153,0 @@ throw new Error(`Cipher must be one of ${cipherTypes.toString()}`); |
{ | ||
"name": "@liskhq/lisk-cryptography", | ||
"version": "4.0.0-beta.2", | ||
"version": "4.0.0-beta.3", | ||
"description": "General cryptographic functions for use with Lisk-related software", | ||
@@ -45,3 +45,3 @@ "author": "Lisk Foundation <admin@lisk.com>, lightcurve GmbH <admin@lightcurve.io>", | ||
"peerDependencies": { | ||
"@chainsafe/blst": "0.2.6", | ||
"@chainsafe/blst": "0.2.9", | ||
"sodium-native": "3.2.1" | ||
@@ -48,0 +48,0 @@ }, |
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
115098
1278