{"version":3,"file":"encryptor.js","names":["crypto","require","Encryptor","hash","algorithm","createHash","_len","arguments","length","buffers","Array","_key","update","Buffer","concat","digest","convertPasswordToHash","password","hashAlgorithm","saltValue","spinCount","toLowerCase","hashes","getHashes","indexOf","Error","passwordBuffer","from","key","i","iterator","alloc","writeUInt32LE","toString","randomBytes","size","module","exports"],"sources":["../../../lib/utils/encryptor.js"],"sourcesContent":["'use strict';\n\nconst crypto = require('crypto');\n\nconst Encryptor = {\n /**\n * Calculate a hash of the concatenated buffers with the given algorithm.\n * @param {string} algorithm - The hash algorithm.\n * @returns {Buffer} The hash\n */\n hash(algorithm, ...buffers) {\n const hash = crypto.createHash(algorithm);\n hash.update(Buffer.concat(buffers));\n return hash.digest();\n },\n /**\n * Convert a password into an encryption key\n * @param {string} password - The password\n * @param {string} hashAlgorithm - The hash algoritm\n * @param {string} saltValue - The salt value\n * @param {number} spinCount - The spin count\n * @param {number} keyBits - The length of the key in bits\n * @param {Buffer} blockKey - The block key\n * @returns {Buffer} The encryption key\n */\n convertPasswordToHash(password, hashAlgorithm, saltValue, spinCount) {\n hashAlgorithm = hashAlgorithm.toLowerCase();\n const hashes = crypto.getHashes();\n if (hashes.indexOf(hashAlgorithm) < 0) {\n throw new Error(`Hash algorithm '${hashAlgorithm}' not supported!`);\n }\n\n // Password must be in unicode buffer\n const passwordBuffer = Buffer.from(password, 'utf16le');\n // Generate the initial hash\n let key = this.hash(hashAlgorithm, Buffer.from(saltValue, 'base64'), passwordBuffer);\n // Now regenerate until spin count\n for (let i = 0; i < spinCount; i++) {\n const iterator = Buffer.alloc(4);\n // this is the 'special' element of Excel password hashing\n // that stops us from using crypto.pbkdf2()\n iterator.writeUInt32LE(i, 0);\n key = this.hash(hashAlgorithm, key, iterator);\n }\n return key.toString('base64');\n },\n /**\n * Generates cryptographically strong pseudo-random data.\n * @param size The size argument is a number indicating the number of bytes to generate.\n */\n randomBytes(size) {\n return crypto.randomBytes(size);\n },\n};\nmodule.exports = Encryptor;\n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