diff --git a/auth/index.html b/auth/index.html
index 24a4cd4..4e69ad5 100644
--- a/auth/index.html
+++ b/auth/index.html
@@ -208,16 +208,23 @@
Message log
};
/**
- * 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}
*/
- var uint8arrayFromHexString = function(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: "' + hexString + '"');
}
- 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;
}
/**
@@ -479,9 +486,9 @@ Message log
{
kty: "EC",
crv: "P-256",
- d: bigIntToBase64Url(privateKey),
- x: bigIntToBase64Url(publicKeyPoint.x.num),
- y: bigIntToBase64Url(publicKeyPoint.y.num),
+ d: bigIntToBase64Url(privateKey, 32),
+ x: bigIntToBase64Url(publicKeyPoint.x.num, 32),
+ y: bigIntToBase64Url(publicKeyPoint.y.num, 32),
ext: true,
},
{
@@ -496,21 +503,22 @@ Message log
/**
* Converts a `BigInt` into a base64url encoded string
* @param {BigInt} num
+ * @param {number} length: optional number of bytes contained in the resulting string
* @return {string}
*/
- var bigIntToBase64Url = function(num) {
+ var bigIntToBase64Url = function(num, length) {
var hexString = num.toString(16);
// Add an extra 0 to the start of the string to get a valid hex string (even length)
// (e.g. 0x0123 instead of 0x123)
var hexString = hexString.padStart(Math.ceil(hexString.length/2)*2, 0)
- var buffer = uint8arrayFromHexString(hexString);
+ var buffer = uint8arrayFromHexString(hexString, length);
return base64urlEncode(buffer)
}
/**
* Converts a `BigInt` into a hex encoded string
* @param {BigInt} num
- * @param {number} length expected length of the resulting hex string
+ * @param {number} length: expected length of the resulting hex string
* @return {string}
*/
var bigIntToHex = function(num, length) {
diff --git a/auth/index.test.js b/auth/index.test.js
index 525a4cf..7190493 100644
--- a/auth/index.test.js
+++ b/auth/index.test.js
@@ -95,16 +95,16 @@ describe("TKHQ", () => {
it("compresses raw P-256 public keys", async () => {
let compressed02 = TKHQ.compressRawPublicKey(TKHQ.uint8arrayFromHexString("04c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4f510c344715f84cf0ba0cc71bd04136c0fb2633a3f459e68ffb8620be16900f0"));
- expect(compressed02).toEqual(TKHQ.uint8arrayFromHexString("02c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4", "hex"));
+ expect(compressed02).toEqual(TKHQ.uint8arrayFromHexString("02c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4"));
let compressed03 = TKHQ.compressRawPublicKey(TKHQ.uint8arrayFromHexString("04be3c8147b75405c94e24280a1759374688bf689549cc1c0afd8e8af20621d734dab002b3cced5db9d9cd343b7d2197c757f42dea13f6689b3553ab1c667a8c67"));
- expect(compressed03).toEqual(TKHQ.uint8arrayFromHexString("03be3c8147b75405c94e24280a1759374688bf689549cc1c0afd8e8af20621d734", "hex"));
+ expect(compressed03).toEqual(TKHQ.uint8arrayFromHexString("03be3c8147b75405c94e24280a1759374688bf689549cc1c0afd8e8af20621d734"));
})
it("uncompresses raw P-256 public keys", async () => {
let uncompressedFrom02 = TKHQ.uncompressRawPublicKey(TKHQ.uint8arrayFromHexString("02c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4"));
- expect(uncompressedFrom02).toEqual(TKHQ.uint8arrayFromHexString("04c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4f510c344715f84cf0ba0cc71bd04136c0fb2633a3f459e68ffb8620be16900f0", "hex"));
+ expect(uncompressedFrom02).toEqual(TKHQ.uint8arrayFromHexString("04c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4f510c344715f84cf0ba0cc71bd04136c0fb2633a3f459e68ffb8620be16900f0"));
let uncompressedFrom03 = TKHQ.uncompressRawPublicKey(TKHQ.uint8arrayFromHexString("03be3c8147b75405c94e24280a1759374688bf689549cc1c0afd8e8af20621d734"));
- expect(uncompressedFrom03).toEqual(TKHQ.uint8arrayFromHexString("04be3c8147b75405c94e24280a1759374688bf689549cc1c0afd8e8af20621d734dab002b3cced5db9d9cd343b7d2197c757f42dea13f6689b3553ab1c667a8c67", "hex"));
+ expect(uncompressedFrom03).toEqual(TKHQ.uint8arrayFromHexString("04be3c8147b75405c94e24280a1759374688bf689549cc1c0afd8e8af20621d734dab002b3cced5db9d9cd343b7d2197c757f42dea13f6689b3553ab1c667a8c67"));
})
it("contains p256JWKPrivateToPublic", async () => {
@@ -178,6 +178,10 @@ describe("TKHQ", () => {
expect(() => {
TKHQ.uint8arrayFromHexString("oops");
}).toThrow('cannot create uint8array from invalid hex string: "oops"');
+ // Happy path: if length parameter is included, pad the resulting buffer
+ expect(TKHQ.uint8arrayFromHexString("01", 2).toString()).toEqual("0,1");
+ // Happy path: if length parameter is omitted, do not pad the resulting buffer
+ expect(TKHQ.uint8arrayFromHexString("01").toString()).toEqual("1");
})
it("contains bigIntToHex", () => {