Last active
July 5, 2023 15:14
-
-
Save 5HT/79a8e26f38f6fe2464be67228b348ec7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EMPTY_BUFFER = new ArrayBuffer(0); | |
console.log("OK"); | |
const key = new Uint8Array([0, 23, 75, 69, 228, 151, 69, 27, 32, 251, 44, 195, 58, 217, 225, 184, 169, 242, 86, 179, 98, 202, 182, 149, 194, 58, 0, 63, 18, 87, 112, 173]).buffer; | |
const keyLen = 256; | |
const info = new Uint8Array([48, 89, 48, 11, 6, 9, 96, 134, 72, 1, 101, 3, 4, 1, 45, 160, 66, 4, 64, 223, 26, 197, 93, 44, 41, 6, 2, 221, 131, 15, 3, 178, 246, 255, 171, 206, 252, 81, 162, 246, 68, 56, 165, 0, 140, 202, 2, 197, 115, 90, 94, 50, 142, 83, 129, 17, 198, 186, 108, 44, 206, 149, 11, 121, 229, 163, 32, 246, 65, 16, 92, 91, 104, 245, 32, 88, 223, 70, 116, 34, 53, 178, 249, 162, 6, 4, 4, 0, 0, 1, 0]).buffer; | |
const hash = "SHA-512"; | |
async function run() { | |
var res = kdf(hash, key, keyLen, info, getCrypto()); | |
return res.then(function (res) { console.log(new Uint8Array(res)) }); | |
// Expected Result on the given data: | |
// 158, 112, 42, 108, 7, 219, 181, 165, 26, 51, 11, 157, 0, 247, 3, 55, 35, 213, 59, | |
// 45, 1, 207, 115, 67, 221, 229, 160, 99, 29, 119, 22, 74 | |
} | |
async function kdf(hashFunction, Zbuffer, keydatalen, SharedInfo, crypto) { | |
const incomingResult = []; | |
let hashLength = 0; | |
let maxCounter = 1; | |
switch (hashFunction.toUpperCase()) { | |
case "SHA-1": hashLength = 160; break; | |
case "SHA-256": hashLength = 256; break; | |
case "SHA-384": hashLength = 384; break; | |
case "SHA-512": hashLength = 512; break; | |
default: console.log('Unknown hash function'); | |
} | |
if (Zbuffer.byteLength === 0) console.log("'Zbuffer' has zero length, error"); | |
const quotient = keydatalen / hashLength; | |
if (Math.floor(quotient) > 0) { | |
maxCounter = Math.floor(quotient); | |
if ((quotient - maxCounter) > 0) | |
maxCounter++; | |
} | |
for (let i = 1; i <= maxCounter; i++) { | |
incomingResult.push(await kdfWithCounter(hashFunction, Zbuffer, i, SharedInfo, crypto)); | |
} | |
let combinedBuffer = EMPTY_BUFFER; | |
let currentCounter = 1; | |
let found = true; | |
while (found) { | |
found = false; | |
for (const result of incomingResult) { | |
if (result.counter === currentCounter) { | |
combinedBuffer = utilConcatBuf(combinedBuffer, result.result); | |
found = true; | |
break; | |
} | |
} | |
currentCounter++; | |
} | |
keydatalen >>= 3; | |
if (combinedBuffer.byteLength > keydatalen) { | |
const newBuffer = new ArrayBuffer(keydatalen); | |
const newView = new Uint8Array(newBuffer); | |
const combinedView = new Uint8Array(combinedBuffer); | |
for (let i = 0; i < keydatalen; i++) newView[i] = combinedView[i]; | |
return newBuffer; | |
} | |
return combinedBuffer; | |
} | |
async function kdfWithCounter(hashFunction, zBuffer, Counter, SharedInfo, crypto) { | |
switch (hashFunction.toUpperCase()) { | |
case "SHA-1": case "SHA-256": case "SHA-384": case "SHA-512": break; | |
default: console.log('Unknown hash function'); | |
} | |
if (zBuffer.byteLength === 0) console.log("'zBuffer' has zero length, error"); | |
if (Counter > 255) console.log("Please set 'Counter' argument to value less or equal to 255"); | |
const counterBuffer = new ArrayBuffer(4); | |
const counterView = new Uint8Array(counterBuffer); | |
counterView[0] = 0x00; | |
counterView[1] = 0x00; | |
counterView[2] = 0x00; | |
counterView[3] = Counter; | |
let combinedBuffer = EMPTY_BUFFER; | |
combinedBuffer = utilConcatBuf(combinedBuffer, zBuffer); | |
combinedBuffer = utilConcatBuf(combinedBuffer, counterBuffer); | |
combinedBuffer = utilConcatBuf(combinedBuffer, SharedInfo); | |
const result = await crypto.digest({ name: hashFunction }, combinedBuffer); | |
return { | |
counter: Counter, | |
result | |
}; | |
} | |
function getCrypto() { | |
return crypto.subtle; | |
} | |
function utilConcatBuf(...buffers) | |
{ | |
let outputLength = 0; | |
let prevLength = 0; | |
for(const buffer of buffers) outputLength += buffer.byteLength; | |
const retBuf = new ArrayBuffer(outputLength); | |
const retView = new Uint8Array(retBuf); | |
for(const buffer of buffers) { | |
retView.set(new Uint8Array(buffer), prevLength); | |
prevLength += buffer.byteLength; | |
} | |
return retBuf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment