j4
j43y ago

Getting error `Unrecognized algorithm name` on `crypto.subtle.importKey()`

I can't figure this one out. I've done some searching and found a list of supported algos at https://deno.land/std@0.167.0/crypto/mod.ts See anything I'm doing wrong? I tried it without the hash as well. Here's a snippet:
/* supabase edge function */
import { crypto } from 'https://deno.land/std@0.167.0/crypto/mod.ts'

const cryptokey = await crypto.subtle.importKey('spki', encoded_plain_key, 'SHA-512', false, ['sign'])
/* supabase edge function */
import { crypto } from 'https://deno.land/std@0.167.0/crypto/mod.ts'

const cryptokey = await crypto.subtle.importKey('spki', encoded_plain_key, 'SHA-512', false, ['sign'])
1 Reply
j4
j4OP3y ago
I've now tried passing in an eliptical curve params object, instead of 'SHA-512'. This ends up throwing an undefined error.
{
name: 'ECDSA',
namedCurve: 'P-521'
}
{
name: 'ECDSA',
namedCurve: 'P-521'
}
I also changed spki to pkcs8, as I realized this is for a private key. Found out I need to base64 decode the key, before I convert it to an array buffer. But even then, undefined is thrown. Thoughts?
/* decode base64 key */
const decoded_key = decode64ToString(Deno.env.get('SIGNATURE_PRIVATE_KEY') || '')
console.log('decoded base64', decoded_key)

/* convert decoded key into ArrayBuffer */
const encoder = new TextEncoder()
const buffer_key = encoder.encode(decoded_key)
console.log('ArrayBuffer key', buffer_key)

/* create a CryptoKey */
let key
try {
key = await crypto.subtle.importKey('pkcs8', buffer_key, {
name: 'ECDSA',
namedCurve: 'P-521',
hash: 'SHA-512'
}, false, ['sign'])

console.log('key', key)
} catch(err) {
console.log('key error', err)
throw err
}

/* later on, sign message */
/* decode base64 key */
const decoded_key = decode64ToString(Deno.env.get('SIGNATURE_PRIVATE_KEY') || '')
console.log('decoded base64', decoded_key)

/* convert decoded key into ArrayBuffer */
const encoder = new TextEncoder()
const buffer_key = encoder.encode(decoded_key)
console.log('ArrayBuffer key', buffer_key)

/* create a CryptoKey */
let key
try {
key = await crypto.subtle.importKey('pkcs8', buffer_key, {
name: 'ECDSA',
namedCurve: 'P-521',
hash: 'SHA-512'
}, false, ['sign'])

console.log('key', key)
} catch(err) {
console.log('key error', err)
throw err
}

/* later on, sign message */

Did you find this page helpful?