Skip to content

Commit 675361a

Browse files
committed
[add] Implement thumbprint for symmetric keys
1 parent 22da01d commit 675361a

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Base64 } from "js-base64"
33

44
export function canonicalizeJwk(jwk: JsonWebKey & {kty: "RSA"}): JsonWebKey;
55
export function canonicalizeJwk(jwk: JsonWebKey & {kty: "EC"}): JsonWebKey;
6+
export function canonicalizeJwk(jwk: JsonWebKey & {kty: "oct"}): JsonWebKey;
67
export function canonicalizeJwk(jwk: JsonWebKey): JsonWebKey | undefined;
78

89
/**
@@ -25,6 +26,11 @@ export function canonicalizeJwk(jwk: JsonWebKey): JsonWebKey | undefined {
2526
x: jwk.x,
2627
y: jwk.y
2728
};
29+
case "oct":
30+
return {
31+
k: jwk.k,
32+
kty: jwk.kty,
33+
};
2834
default:
2935
return undefined;
3036
}
@@ -42,7 +48,7 @@ type EcType = {
4248
base64url: string
4349
}
4450

45-
export function jwkThumbprintByEncoding<Ec extends Encodings>(jwk: JsonWebKey & {kty: "RSA" | "EC"}, hashAlg: HashAlg, ec: Ec): EcType[Ec];
51+
export function jwkThumbprintByEncoding<Ec extends Encodings>(jwk: JsonWebKey & {kty: "RSA" | "EC" | "oct"}, hashAlg: HashAlg, ec: Ec): EcType[Ec];
4652
export function jwkThumbprintByEncoding<Ec extends Encodings>(jwk: JsonWebKey, hashAlg: HashAlg, ec: Ec): EcType[Ec] | undefined;
4753

4854
/**
@@ -97,7 +103,7 @@ export function jwkThumbprintByEncoding<Ec extends Encodings>(jwk: JsonWebKey, h
97103
}
98104
}
99105

100-
export function jwkThumbprint<Ec extends Encodings>(jwk: JsonWebKey & {kty: "RSA" | "EC"}, hashAlg: HashAlg): Uint8Array;
106+
export function jwkThumbprint<Ec extends Encodings>(jwk: JsonWebKey & {kty: "RSA" | "EC" | "oct"}, hashAlg: HashAlg): Uint8Array;
101107
export function jwkThumbprint<Ec extends Encodings>(jwk: JsonWebKey, hashAlg: HashAlg): Uint8Array | undefined;
102108

103109
/**

test/index.test.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as assert from 'power-assert';
44
describe('jwk-thumbprint', () => {
55
it('should return the same thumbprint as an example in RFC7638', () => {
66
// (from: https://tools.ietf.org/html/rfc7638#section-3.1)
7-
const jwk: JsonWebKey & {kty: "RSA", kid: string} = {
7+
const jwk: JsonWebKey & { kty: "RSA", kid: string } = {
88
"kty": "RSA",
99
"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
1010
"e": "AQAB",
@@ -32,7 +32,7 @@ describe('jwk-thumbprint', () => {
3232
// NOTE: The return-type must not be undefined
3333
const actual: Uint8Array = jwkThumbprint(jwk, "SHA-256");
3434
// NOTE: This expectation is also on https://tools.ietf.org/html/rfc7638#section-3.1
35-
const expect = new Uint8Array( expectNumbers);
35+
const expect = new Uint8Array(expectNumbers);
3636
assert.deepStrictEqual(actual, expect);
3737
}
3838

@@ -44,4 +44,31 @@ describe('jwk-thumbprint', () => {
4444
assert.deepStrictEqual(actual, expect);
4545
}
4646
});
47+
48+
it('should return symmetric key thumbprint', () => {
49+
const jwk: JsonWebKey & { kty: "oct" } = {
50+
"alg": "A128GCM",
51+
"ext": true,
52+
"k": "9wcPr5BVF6hku5Fx3IrejQ",
53+
"key_ops": ["encrypt", "decrypt"],
54+
"kty": "oct"
55+
};
56+
57+
// NOTE: The return-type must not be undefined
58+
const actual: string = jwkThumbprintByEncoding(jwk, "SHA-256", 'base64url');
59+
// NOTE: This expectation is calculated by a Ruby library, 'json/jwt'
60+
//
61+
// require 'json/jwt'
62+
//
63+
// jwk = JSON::JWK.new ({
64+
// "alg": "A128GCM",
65+
// "ext": true,
66+
// "k": "9wcPr5BVF6hku5Fx3IrejQ",
67+
// "key_ops": ["encrypt", "decrypt"],
68+
// "kty": "oct"
69+
// })
70+
// puts(jwk.thumbprint)
71+
const expect = "O0ohsAio8Tj1lGR2SoX3Xa90quibp6j3vSe71e0LXRY";
72+
assert.deepStrictEqual(actual, expect);
73+
});
4774
});

0 commit comments

Comments
 (0)