PHP WebShell

Текущая директория: /opt/BitGoJS/node_modules/@hashgraph/cryptography/lib/primitive

Просмотр файла: keystore.cjs

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createKeystore = createKeystore;
exports.loadKeystore = loadKeystore;

var _BadKeyError = _interopRequireDefault(require("../BadKeyError.cjs"));

var crypto = _interopRequireWildcard(require("./aes.cjs"));

var hex = _interopRequireWildcard(require("../encoding/hex.cjs"));

var utf8 = _interopRequireWildcard(require("../encoding/utf8.cjs"));

var hmac = _interopRequireWildcard(require("./hmac.cjs"));

var pbkdf2 = _interopRequireWildcard(require("./pbkdf2.cjs"));

var random = _interopRequireWildcard(require("./random.cjs"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const HMAC_SHA256 = "hmac-sha256";
/**
 * @typedef {Object} KeystoreKdfParams
 * @property {number} dkLen
 * @property {string} salt
 * @property {number} c
 * @property {string} prf
 */

/**
 * @typedef {Object} KeystoreCipherParams
 * @property {string} iv
 */

/**
 * @typedef {Object} KeystoreCrypto
 * @property {string} ciphertext
 * @property {KeystoreCipherParams} cipherparams
 * @property {string} cipher
 * @property {string} kdf
 * @property {KeystoreKdfParams} kdfparams
 * @property {string} mac
 */

/**
 * @typedef {Object} Keystore
 * @property {number} version
 * @property {KeystoreCrypto} crypto
 */

/**
 * @param {Uint8Array} privateKey
 * @param {string} passphrase
 * @returns {Promise<Uint8Array>}
 */

async function createKeystore(privateKey, passphrase) {
  // all values taken from https://github.com/ethereumjs/ethereumjs-wallet/blob/de3a92e752673ada1d78f95cf80bc56ae1f59775/src/index.ts#L25
  const dkLen = 32;
  const c = 262144;
  const saltLen = 32;
  const salt = await random.bytesAsync(saltLen);
  const key = await pbkdf2.deriveKey(hmac.HashAlgorithm.Sha256, passphrase, salt, c, dkLen);
  const iv = await random.bytesAsync(16); // AES-128-CTR with the first half of the derived key and a random IV

  const cipherText = await crypto.createCipheriv(crypto.CipherAlgorithm.Aes128Ctr, key.slice(0, 16), iv, privateKey);
  const mac = await hmac.hash(hmac.HashAlgorithm.Sha384, key.slice(16), cipherText);
  /**
   * @type {Keystore}
   */

  const keystore = {
    version: 1,
    crypto: {
      ciphertext: hex.encode(cipherText),
      cipherparams: {
        iv: hex.encode(iv)
      },
      cipher: crypto.CipherAlgorithm.Aes128Ctr,
      kdf: "pbkdf2",
      kdfparams: {
        dkLen,
        salt: hex.encode(salt),
        c,
        prf: HMAC_SHA256
      },
      mac: hex.encode(mac)
    }
  };
  return utf8.encode(JSON.stringify(keystore));
}
/**
 * @param {Uint8Array} keystoreBytes
 * @param {string} passphrase
 * @returns {Promise<Uint8Array>}
 */


async function loadKeystore(keystoreBytes, passphrase) {
  /**
   * @type {Keystore}
   */
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  const keystore = JSON.parse(utf8.decode(keystoreBytes));

  if (keystore.version !== 1) {
    throw new _BadKeyError.default(`unsupported keystore version: ${keystore.version}`);
  }

  const {
    ciphertext,
    cipherparams: {
      iv
    },
    cipher,
    kdf,
    kdfparams: {
      dkLen,
      salt,
      c,
      prf
    },
    mac
  } = keystore.crypto;

  if (kdf !== "pbkdf2") {
    throw new _BadKeyError.default(`unsupported key derivation function:" + ${kdf}`);
  }

  if (prf !== HMAC_SHA256) {
    throw new _BadKeyError.default(`unsupported key derivation hash function: ${prf}`);
  }

  const saltBytes = hex.decode(salt);
  const ivBytes = hex.decode(iv);
  const cipherBytes = hex.decode(ciphertext);
  const key = await pbkdf2.deriveKey(hmac.HashAlgorithm.Sha256, passphrase, saltBytes, c, dkLen);
  const macHex = hex.decode(mac);
  const verifyHmac = await hmac.hash(hmac.HashAlgorithm.Sha384, key.slice(16), cipherBytes); // compare that these two Uint8Arrays are equivalent

  if (!macHex.every((b, i) => b === verifyHmac[i])) {
    throw new _BadKeyError.default("HMAC mismatch; passphrase is incorrect");
  }

  return crypto.createDecipheriv(cipher, key.slice(0, 16), ivBytes, cipherBytes);
}

Выполнить команду


Для локальной разработки. Не используйте в интернете!