@@ -5,17 +5,14 @@ module Crypto.Subtle.Key.Derive
5
5
, DeriveTargetAlgorithm , hmac , aes
6
6
) where
7
7
8
- import Crypto.Subtle.Key.Types (CryptoKey , CryptoKeyUsage )
9
- import Crypto.Subtle.Hash (HashingFunction )
10
- import Crypto.Subtle.Constants.EC (ECAlgorithm )
8
+ import Control.Promise (Promise , toAff' )
11
9
import Crypto.Subtle.Constants.AES (AESAlgorithm , AESBitLength )
12
-
13
- import Prelude ((<<<), (<$) )
14
- import Data.Function.Uncurried ( Fn3 , Fn5 , runFn3 , runFn5 )
10
+ import Crypto.Subtle.Constants.EC ( ECAlgorithm )
11
+ import Crypto.Subtle.Hash ( HashingFunction )
12
+ import Crypto.Subtle.Key.Types ( CryptoKey , CryptoKeyUsage , errorFromDOMException )
15
13
import Data.ArrayBuffer.Types (ArrayBuffer )
16
- import Data.Either (Either (..))
17
- import Effect.Promise (Promise , runPromise )
18
- import Effect.Aff (Aff , makeAff , nonCanceler )
14
+ import Data.Function.Uncurried (Fn3 , Fn5 , runFn3 , runFn5 )
15
+ import Effect.Aff (Aff )
19
16
import Unsafe.Coerce (unsafeCoerce )
20
17
21
18
@@ -24,38 +21,41 @@ foreign import deriveKeyImpl :: Fn5 DeriveAlgorithm CryptoKey DeriveTargetAlgori
24
21
foreign import deriveBitsImpl :: Fn3 DeriveAlgorithm CryptoKey Int (Promise ArrayBuffer )
25
22
26
23
24
+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey
27
25
deriveKey :: DeriveAlgorithm
28
26
-> CryptoKey -- ^ Base key
29
27
-> DeriveTargetAlgorithm
30
28
-> Boolean -- ^ Extractable
31
29
-> Array CryptoKeyUsage
32
30
-> Aff CryptoKey
33
- deriveKey a k t e u = makeAff \resolve ->
34
- nonCanceler <$ runPromise (resolve <<< Right ) (resolve <<< Left ) (runFn5 deriveKeyImpl a k t e u)
31
+ deriveKey a k t e u = toAff' errorFromDOMException (runFn5 deriveKeyImpl a k t e u)
35
32
33
+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
36
34
deriveBits :: DeriveAlgorithm
37
35
-> CryptoKey -- ^ Base key
38
36
-> Int -- ^ Length in bits
39
37
-> Aff ArrayBuffer
40
- deriveBits a k l = makeAff \resolve ->
41
- nonCanceler <$ runPromise (resolve <<< Right ) (resolve <<< Left ) (runFn3 deriveBitsImpl a k l)
38
+ deriveBits a k l = toAff' errorFromDOMException (runFn3 deriveBitsImpl a k l)
42
39
43
40
44
41
foreign import data DeriveAlgorithm :: Type
45
42
foreign import data DeriveTargetAlgorithm :: Type
46
43
47
44
45
+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#ecdh
48
46
ec :: ECAlgorithm
49
47
-> CryptoKey -- ^ Public key of the other entity
50
48
-> DeriveAlgorithm
51
49
ec e k = unsafeCoerce {name: e, public: k}
52
50
51
+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#hkdf
53
52
hkdf :: HashingFunction
54
53
-> ArrayBuffer -- ^ Salt
55
54
-> ArrayBuffer -- ^ Info
56
55
-> DeriveAlgorithm
57
56
hkdf h s i = unsafeCoerce {name: " HKDF" , hash: h, salt: s, info: i}
58
57
58
+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#pbkdf2
59
59
pbkdf2 :: HashingFunction
60
60
-> ArrayBuffer -- ^ Salt
61
61
-> Int -- ^ Iterations
@@ -65,8 +65,10 @@ pbkdf2 h s i = unsafeCoerce {name: "PBKDF2", hash: h, salt: s, iterations: i}
65
65
66
66
67
67
68
+ -- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams
68
69
hmac :: HashingFunction -> DeriveTargetAlgorithm
69
70
hmac h = unsafeCoerce {name: " HMAC" , hash: h}
70
71
72
+ -- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams
71
73
aes :: AESAlgorithm -> AESBitLength -> DeriveTargetAlgorithm
72
74
aes a l = unsafeCoerce {name: a, length: l}
0 commit comments