How to use @ethersproject/pbkdf2 - 8 common examples

To help you get started, we’ve selected a few @ethersproject/pbkdf2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ethers-io / ethers.js / packages / json-wallets / lib / crowdsale.js View on Github external
function decrypt(json, password) {
    var data = JSON.parse(json);
    password = utils_1.getPassword(password);
    // Ethereum Address
    var ethaddr = address_1.getAddress(utils_1.searchPath(data, "ethaddr"));
    // Encrypted Seed
    var encseed = utils_1.looseArrayify(utils_1.searchPath(data, "encseed"));
    if (!encseed || (encseed.length % 16) !== 0) {
        logger.throwArgumentError("invalid encseed", "json", json);
    }
    var key = bytes_1.arrayify(pbkdf2_1.pbkdf2(password, password, 2000, 32, "sha256")).slice(0, 16);
    var iv = encseed.slice(0, 16);
    var encryptedSeed = encseed.slice(16);
    // Decrypt the seed
    var aesCbc = new aes_js_1.default.ModeOfOperation.cbc(key, iv);
    var seed = aes_js_1.default.padding.pkcs7.strip(bytes_1.arrayify(aesCbc.decrypt(encryptedSeed)));
    // This wallet format is weird... Convert the binary encoded hex to a string.
    var seedHex = "";
    for (var i = 0; i < seed.length; i++) {
        seedHex += String.fromCharCode(seed[i]);
    }
    var seedHexBytes = strings_1.toUtf8Bytes(seedHex);
    var privateKey = keccak256_1.keccak256(seedHexBytes);
    return new CrowdsaleAccount({
        _isCrowdsaleAccount: true,
        address: ethaddr,
        privateKey: privateKey
github ethers-io / ethers.js / packages / json-wallets / src.ts / crowdsale.ts View on Github external
export function decrypt(json: string, password: Bytes | string): ExternallyOwnedAccount {
    const data = JSON.parse(json);

    password = getPassword(password);

    // Ethereum Address
    const ethaddr = getAddress(searchPath(data, "ethaddr"));

    // Encrypted Seed
    const encseed = looseArrayify(searchPath(data, "encseed"));
    if (!encseed || (encseed.length % 16) !== 0) {
        logger.throwArgumentError("invalid encseed", "json", json);
    }

    const key = arrayify(pbkdf2(password, password, 2000, 32, "sha256")).slice(0, 16);

    const iv = encseed.slice(0, 16);
    const encryptedSeed = encseed.slice(16);

    // Decrypt the seed
    const aesCbc = new aes.ModeOfOperation.cbc(key, iv);
    const seed = aes.padding.pkcs7.strip(arrayify(aesCbc.decrypt(encryptedSeed)));

    // This wallet format is weird... Convert the binary encoded hex to a string.
    let seedHex = "";
    for (let i = 0; i < seed.length; i++) {
        seedHex += String.fromCharCode(seed[i]);
    }

    const seedHexBytes = toUtf8Bytes(seedHex);
github ethers-io / ethers.js / packages / hdnode / lib.esm / index.js View on Github external
export function mnemonicToSeed(mnemonic, password) {
    if (!password) {
        password = "";
    }
    const salt = toUtf8Bytes("mnemonic" + password, UnicodeNormalizationForm.NFKD);
    return pbkdf2(toUtf8Bytes(mnemonic, UnicodeNormalizationForm.NFKD), salt, 2048, 64, "sha512");
}
export function mnemonicToEntropy(mnemonic, wordlist) {
github ethers-io / ethers.js / packages / json-wallets / lib.esm / keystore.js View on Github external
const prf = searchPath(data, "crypto/kdfparams/prf");
                if (prf === "hmac-sha256") {
                    prfFunc = "sha256";
                }
                else if (prf === "hmac-sha512") {
                    prfFunc = "sha512";
                }
                else {
                    throw new Error("unsupported prf");
                }
                const c = parseInt(searchPath(data, "crypto/kdfparams/c"));
                const dkLen = parseInt(searchPath(data, "crypto/kdfparams/dklen"));
                if (dkLen !== 32) {
                    throw new Error("unsupported key-derivation derived-key length");
                }
                const key = arrayify(pbkdf2(passwordBytes, salt, c, dkLen, prfFunc));
                return getAccount(key);
            }
        }
        throw new Error("unsupported key-derivation function");
    });
}
github ethers-io / ethers.js / packages / hdnode / src.ts / index.ts View on Github external
export function mnemonicToSeed(mnemonic: string, password?: string): string {
    if (!password) { password = ""; }

    const salt = toUtf8Bytes("mnemonic" + password, UnicodeNormalizationForm.NFKD);

    return pbkdf2(toUtf8Bytes(mnemonic, UnicodeNormalizationForm.NFKD), salt, 2048, 64, "sha512");
}
github ethers-io / ethers.js / packages / json-wallets / lib / keystore.js View on Github external
prf = utils_1.searchPath(data, "crypto/kdfparams/prf");
                        if (prf === "hmac-sha256") {
                            prfFunc = "sha256";
                        }
                        else if (prf === "hmac-sha512") {
                            prfFunc = "sha512";
                        }
                        else {
                            throw new Error("unsupported prf");
                        }
                        c = parseInt(utils_1.searchPath(data, "crypto/kdfparams/c"));
                        dkLen = parseInt(utils_1.searchPath(data, "crypto/kdfparams/dklen"));
                        if (dkLen !== 32) {
                            throw new Error("unsupported key-derivation derived-key length");
                        }
                        key = bytes_1.arrayify(pbkdf2_1.pbkdf2(passwordBytes, salt, c, dkLen, prfFunc));
                        return [2 /*return*/, getAccount(key)];
                    }
                    _a.label = 3;
                case 3: throw new Error("unsupported key-derivation function");
            }
        });
    });
github ethers-io / ethers.js / packages / hdnode / lib / index.js View on Github external
function mnemonicToSeed(mnemonic, password) {
    if (!password) {
        password = "";
    }
    var salt = strings_1.toUtf8Bytes("mnemonic" + password, strings_1.UnicodeNormalizationForm.NFKD);
    return pbkdf2_1.pbkdf2(strings_1.toUtf8Bytes(mnemonic, strings_1.UnicodeNormalizationForm.NFKD), salt, 2048, 64, "sha512");
}
exports.mnemonicToSeed = mnemonicToSeed;
github ethers-io / ethers.js / packages / json-wallets / src.ts / keystore.ts View on Github external
if (prf === "hmac-sha256") {
                prfFunc = "sha256";
            } else if (prf === "hmac-sha512") {
                prfFunc = "sha512";
            } else {
                throw new Error("unsupported prf");
            }

            const c = parseInt(searchPath(data, "crypto/kdfparams/c"));

            const dkLen = parseInt(searchPath(data, "crypto/kdfparams/dklen"));
            if (dkLen !== 32) {
                throw new Error("unsupported key-derivation derived-key length");
            }

            const key = arrayify(pbkdf2(passwordBytes, salt, c, dkLen, prfFunc));

            return getAccount(key);
        }
    }
    throw new Error("unsupported key-derivation function");
}

@ethersproject/pbkdf2

The PBKDF2 password-pbased key derivation function for ethers.

MIT
Latest version published 2 years ago

Package Health Score

65 / 100
Full package analysis

Popular @ethersproject/pbkdf2 functions