UNPKG

iobroker.vw-connect

Version:
41 lines (35 loc) 1.4 kB
const crypto = require("crypto"); const { Crypto } = require("@peculiar/webcrypto"); const toHexString = function(byteArray) { return Array.prototype.map .call(byteArray, function(byte) { return ("0" + (byte & 0xff).toString(16).toUpperCase()).slice(-2); }) .join(""); } const toByteArray = function(hexString) { const result = []; for (let i = 0; i < hexString.length; i += 2) { result.push(parseInt(hexString.substr(i, 2), 16)); } return result; } const generateSecurPin = function(challenge) { return new Promise((resolve, reject) => { const pin = "3364"; const byteChallenge = toByteArray(challenge); const webcrypto = new Crypto(); const concat = new Int8Array(pin.concat(byteChallenge)); const digest = webcrypto.subtle.digest("SHA-512", concat).then(digest => { const utf8Array = new Int8Array(digest); resolve(toHexString(utf8Array)); }); }); } generateSecurPin("3680E2E102C45655318BE5660D6E43DE7D7CA8FA733BFE983FC071BE5A7B5321").then((value) => console.log(value)) const hash = crypto.createHash("sha512") const buf1 = Buffer.from("3364", "hex") const buf2 = Buffer.from("3680E2E102C45655318BE5660D6E43DE7D7CA8FA733BFE983FC071BE5A7B5321", "hex") const buf = Buffer.concat([buf1, buf2]) hash.update(buf) console.log(hash.digest("hex"))