-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add optional target length #44
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,16 +208,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} | ||
*/ | ||
var uint8arrayFromHexString = function(hexString) { | ||
var uint8arrayFromHexString = function(hexString, length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add a test to exercise this new behavior? e.g. |
||
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 @@ <h2>Message log</h2> | |
{ | ||
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 @@ <h2>Message log</h2> | |
/** | ||
* 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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this |
||
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", () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor:
e4567abc
is of even-length