Skip to content

Commit

Permalink
fix implementation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarshanbhag committed Aug 6, 2024
1 parent f2bd83c commit 513cbce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
19 changes: 13 additions & 6 deletions export/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,23 @@ <h2>Message log</h2>
};

/**
* Takes a hex string (e.g. "e4567ab") and returns an array buffer (Uint8Array)
* Takes a hex string (e.g. "e4567abc") and returns an array buffer (Uint8Array)
* @param {string} hexString
* @param {number} length: optional expected length of the resulting buffer
* @returns {Uint8Array}
*/
function uint8arrayFromHexString(hexString) {
var uint8arrayFromHexString = function(hexString, length) {
var hexRegex = /^[0-9A-Fa-f]+$/;
if (!hexString || hexString.length % 2 != 0 || !hexRegex.test(hexString)) {
throw new Error('cannot create uint8array from invalid hex string');
}
return new Uint8Array(hexString.match(/../g).map(h=>parseInt(h,16)));
var buffer = new Uint8Array(hexString.match(/../g).map((h) => parseInt(h, 16)));
if (!length) {
return buffer;
}
var paddedBuffer = new Uint8Array(length);
paddedBuffer.set(buffer, length - buffer.length);
return paddedBuffer;
}

/**
Expand Down Expand Up @@ -404,7 +411,6 @@ <h2>Message log</h2>
{
kty: jwkPrivateCopy.kty,
crv: jwkPrivateCopy.crv,
d: padBase64Url(jwkPrivateCopy.d, 32),
x: padBase64Url(jwkPrivateCopy.x, 32),
y: padBase64Url(jwkPrivateCopy.y, 32),
ext: jwkPrivateCopy.ext,
Expand All @@ -421,7 +427,7 @@ <h2>Message log</h2>
}

/**
* Converts a `BigInt` into a base64url encoded string
* Decodes a base64url encoded string and pads the hex string correctly, encoding the result into a base64url encoded string
* @param {string} base64UrlString
* @param {number} length: optional number of bytes contained in the resulting string
* @return {string}
Expand Down Expand Up @@ -632,7 +638,8 @@ <h2>Message log</h2>
fromDerSignature,
additionalAssociatedData,
verifyEnclaveSignature,
getEd25519PublicKey
getEd25519PublicKey,
padBase64Url,
}
}();
</script>
Expand Down
5 changes: 5 additions & 0 deletions export/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ describe("TKHQ", () => {
).rejects.toThrow('cannot create uint8array from invalid hex string');
})
});

it("pads jwk keys to correct length", async () => {
expect(TKHQ.padBase64Url("wqVkltEnJAh2mJ5k9rAxEifyKv2BrAWscZ6QZDCS2Os", 32)).toBe("wqVkltEnJAh2mJ5k9rAxEifyKv2BrAWscZ6QZDCS2Os")
expect(TKHQ.padBase64Url("AQ", 32)).toBe("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE")
})

0 comments on commit 513cbce

Please sign in to comment.