diff options
author | Luca Casonato <hello@lcas.dev> | 2021-12-10 22:23:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 22:23:19 +0100 |
commit | cbfc8dd59d79fa6e8c5a59ca97508ea4285ff155 (patch) | |
tree | 586ff050dc320de8cdb68d38e58a481d505c2bcc /ext/crypto/00_crypto.js | |
parent | 2bdb528eb89bae468b802a65338001ac95f8f563 (diff) |
refactor(ext/crypto): symmetric jwk decode in rust (#13047)
Diffstat (limited to 'ext/crypto/00_crypto.js')
-rw-r--r-- | ext/crypto/00_crypto.js | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js index 16c862829..4e2a90f3b 100644 --- a/ext/crypto/00_crypto.js +++ b/ext/crypto/00_crypto.js @@ -12,7 +12,7 @@ const core = window.Deno.core; const webidl = window.__bootstrap.webidl; const { DOMException } = window.__bootstrap.domException; - const { atob, btoa } = window.__bootstrap.base64; + const { btoa } = window.__bootstrap.base64; const { ArrayPrototypeFind, @@ -23,7 +23,6 @@ BigInt64Array, StringPrototypeToUpperCase, StringPrototypeReplace, - StringPrototypeCharCodeAt, StringFromCharCode, Symbol, SymbolFor, @@ -169,23 +168,6 @@ }, }; - // Decodes the unpadded base64 to the octet sequence containing key value `k` defined in RFC7518 Section 6.4 - function decodeSymmetricKey(key) { - // Decode from base64url without `=` padding. - const base64 = StringPrototypeReplace( - StringPrototypeReplace(key, /\-/g, "+"), - /\_/g, - "/", - ); - const decodedKey = atob(base64); - const keyLength = decodedKey.length; - const keyBytes = new Uint8Array(keyLength); - for (let i = 0; i < keyLength; i++) { - keyBytes[i] = StringPrototypeCharCodeAt(decodedKey, i); - } - return keyBytes; - } - function unpaddedBase64(bytes) { let binaryString = ""; for (let i = 0; i < bytes.length; i++) { @@ -1901,7 +1883,12 @@ } // 4. - data = decodeSymmetricKey(jwk.k); + const { rawData } = core.opSync( + "op_crypto_import_key", + { algorithm: "AES" }, + { jwkSecret: jwk }, + ); + data = rawData.data; // 5. switch (data.byteLength * 8) { @@ -2038,6 +2025,7 @@ case "jwk": { // TODO(@littledivy): Why does the spec validate JWK twice? const jwk = keyData; + // 2. if (jwk.kty !== "oct") { throw new DOMException( @@ -2055,7 +2043,12 @@ } // 4. - data = decodeSymmetricKey(jwk.k); + const { rawData } = core.opSync( + "op_crypto_import_key", + { algorithm: "HMAC" }, + { jwkSecret: jwk }, + ); + data = rawData.data; // 5. hash = normalizedAlgorithm.hash; |