mirror of
https://github.com/hanzoai/jose.git
synced 2026-10-30 14:46:24 +00:00
27 lines
733 B
TypeScript
27 lines
733 B
TypeScript
import crypto, { isCryptoKey } from './webcrypto.ts'
|
|
import { checkSigCryptoKey } from '../lib/crypto_key.ts'
|
|
import invalidKeyInput from '../lib/invalid_key_input.ts'
|
|
import { types } from './is_key_like.ts'
|
|
|
|
export default function getCryptoKey(alg: string, key: unknown, usage: KeyUsage) {
|
|
if (isCryptoKey(key)) {
|
|
checkSigCryptoKey(key, alg, usage)
|
|
return key
|
|
}
|
|
|
|
if (key instanceof Uint8Array) {
|
|
if (!alg.startsWith('HS')) {
|
|
throw new TypeError(invalidKeyInput(key, ...types))
|
|
}
|
|
return crypto.subtle.importKey(
|
|
'raw',
|
|
key,
|
|
{ hash: `SHA-${alg.slice(-3)}`, name: 'HMAC' },
|
|
false,
|
|
[usage],
|
|
)
|
|
}
|
|
|
|
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'))
|
|
}
|