@@ -0,0 +1,63 @@
1+module Crypto.Subtle.Key.Import
2+ ( importKey
3+ , ImportFormat, raw, pkcs8, spki, jwk
4+ , ImportAlgorithm, rsa, ec, hmac, aes
5+ ) where
6+
7+import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage)
8+import Crypto.Subtle.Hash (HashingFunction)
9+import Crypto.Subtle.RSA (RSAAlgorithm)
10+import Crypto.Subtle.EC (ECAlgorithm, ECCurve)
11+import Crypto.Subtle.AES (AESAlgorithm)
12+
13+import Prelude ((<<<), (<$))
14+import Data.Function.Uncurried (Fn5, runFn5)
15+import Data.ArrayBuffer.Types (ArrayBuffer)
16+import Data.Either (Either (..))
17+import Effect.Promise (Promise, runPromise)
18+import Effect.Aff (Aff, makeAff, nonCanceler)
19+import Unsafe.Coerce (unsafeCoerce)
20+
21+
22+
23+foreign import importKeyImpl :: Fn5 ImportFormat ArrayBuffer ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey)
24+
25+
26+importKey :: ImportFormat
27+ -> ArrayBuffer -- ^ Key data
28+ -> ImportAlgorithm
29+ -> Boolean -- ^ Extractable
30+ -> Array CryptoKeyUsage
31+ -> Aff CryptoKey
32+importKey f x a e u = makeAff \resolve ->
33+ nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn5 importKeyImpl f x a e u)
34+
35+
36+newtype ImportFormat = ImportFormat String
37+
38+raw :: ImportFormat
39+raw = ImportFormat "raw"
40+pkcs8 :: ImportFormat
41+pkcs8 = ImportFormat "pkcs8"
42+spki :: ImportFormat
43+spki = ImportFormat "spki"
44+jwk :: ImportFormat
45+jwk = ImportFormat "jwk"
46+
47+
48+
49+
50+foreign import data ImportAlgorithm :: Type
51+
52+
53+rsa :: RSAAlgorithm -> HashingFunction -> ImportAlgorithm
54+rsa r h = unsafeCoerce {name: r, hash: h}
55+
56+ec :: ECAlgorithm -> ECCurve -> ImportAlgorithm
57+ec e c = unsafeCoerce {name: e, namedCurve: c}
58+
59+hmac :: HashingFunction -> ImportAlgorithm
60+hmac h = unsafeCoerce {name: "HMAC", hash: h}
61+
62+aes :: AESAlgorithm -> ImportAlgorithm
63+aes a = unsafeCoerce {name: a}