iso-signatures 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @typedef {import('../types').IResolver} IResolver
3
+ */
4
+
5
+ /**
6
+ * No cache
7
+ *
8
+ * @type {import('../types').Cache}
9
+ */
10
+ function noCache(input, verify) {
11
+ return verify(input)
12
+ }
13
+
14
+ /**
15
+ * Memory cache factory
16
+ *
17
+ * @returns {import('../types').Cache}
18
+ */
19
+ function memoryCache() {
20
+ const cache = new Map()
21
+
22
+ /** @type {import('../types').Cache} */
23
+ async function fn(input, verify) {
24
+ const key = JSON.stringify(input)
25
+ if (cache.has(key)) {
26
+ return cache.get(key)
27
+ }
28
+
29
+ const result = await verify(input)
30
+ cache.set(key, result)
31
+ return result
32
+ }
33
+
34
+ return fn
35
+ }
36
+
37
+ /**
38
+ * Verifier resolver
39
+ *
40
+ * @implements {IResolver}
41
+ */
42
+ export class Resolver {
43
+ /**
44
+ *
45
+ * @param {import('../types').VerifierRegistry<import('iso-did/types').SignatureAlgorithm>} registry
46
+ * @param {import('../types').ResolverOptions} options
47
+ */
48
+ constructor(registry = {}, options = {}) {
49
+ this.registry = registry
50
+ this.cache =
51
+ options.cache === true ? memoryCache() : options.cache || noCache
52
+ }
53
+
54
+ /** @type {IResolver['verify']} */
55
+ async verify(input) {
56
+ const verify = this.registry[input.alg]
57
+ if (!verify) {
58
+ throw new TypeError(`Unsupported signature type "${input.alg}"`)
59
+ }
60
+
61
+ return this.cache(input, verify)
62
+ }
63
+ }
@@ -0,0 +1,25 @@
1
+ /* eslint-disable unicorn/numeric-separators-style */
2
+ import { webcrypto } from 'iso-base/crypto'
3
+
4
+ /** @type {import('../types.js').Verify} */
5
+ export async function verify({ signature, message, publicKey }) {
6
+ const key = await webcrypto.subtle.importKey(
7
+ 'spki',
8
+ publicKey,
9
+ { name: 'RSASSA-PKCS1-v1_5', hash: { name: 'SHA-256' } },
10
+ true,
11
+ ['verify']
12
+ )
13
+
14
+ return await webcrypto.subtle.verify(
15
+ { name: 'RSASSA-PKCS1-v1_5', saltLength: 128 },
16
+ key,
17
+ signature,
18
+ message
19
+ )
20
+ }
21
+
22
+ /** @type {import('../types.js').Verifier<'RS256_OLD'>} */
23
+ export const verifier = {
24
+ RS256_OLD: verify,
25
+ }
@@ -0,0 +1,26 @@
1
+ /* eslint-disable unicorn/numeric-separators-style */
2
+ import { webcrypto } from 'iso-base/crypto'
3
+ import { spki } from '../spki.js'
4
+
5
+ /** @type {import('../types.js').Verify} */
6
+ export async function verify({ signature, message, publicKey }) {
7
+ const key = await webcrypto.subtle.importKey(
8
+ 'spki',
9
+ spki.encode(publicKey),
10
+ { name: 'RSASSA-PKCS1-v1_5', hash: { name: 'SHA-256' } },
11
+ true,
12
+ ['verify']
13
+ )
14
+
15
+ return await webcrypto.subtle.verify(
16
+ { name: 'RSASSA-PKCS1-v1_5', saltLength: 128 },
17
+ key,
18
+ signature,
19
+ message
20
+ )
21
+ }
22
+
23
+ /** @type {import('../types.js').Verifier<'RS256'>} */
24
+ export const verifier = {
25
+ RS256: verify,
26
+ }