@@ -5,17 +5,14 @@ module Crypto.Subtle.Key.Derive
55 , DeriveTargetAlgorithm , hmac , aes
66 ) where
77
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' )
119import 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 )
1513import 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 )
1916import Unsafe.Coerce (unsafeCoerce )
2017
2118
@@ -24,38 +21,41 @@ foreign import deriveKeyImpl :: Fn5 DeriveAlgorithm CryptoKey DeriveTargetAlgori
2421foreign import deriveBitsImpl :: Fn3 DeriveAlgorithm CryptoKey Int (Promise ArrayBuffer )
2522
2623
24+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey
2725deriveKey :: DeriveAlgorithm
2826 -> CryptoKey -- ^ Base key
2927 -> DeriveTargetAlgorithm
3028 -> Boolean -- ^ Extractable
3129 -> Array CryptoKeyUsage
3230 -> 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)
3532
33+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
3634deriveBits :: DeriveAlgorithm
3735 -> CryptoKey -- ^ Base key
3836 -> Int -- ^ Length in bits
3937 -> 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)
4239
4340
4441foreign import data DeriveAlgorithm :: Type
4542foreign import data DeriveTargetAlgorithm :: Type
4643
4744
45+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#ecdh
4846ec :: ECAlgorithm
4947 -> CryptoKey -- ^ Public key of the other entity
5048 -> DeriveAlgorithm
5149ec e k = unsafeCoerce {name: e, public: k}
5250
51+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#hkdf
5352hkdf :: HashingFunction
5453 -> ArrayBuffer -- ^ Salt
5554 -> ArrayBuffer -- ^ Info
5655 -> DeriveAlgorithm
5756hkdf h s i = unsafeCoerce {name: " HKDF" , hash: h, salt: s, info: i}
5857
58+ -- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#pbkdf2
5959pbkdf2 :: HashingFunction
6060 -> ArrayBuffer -- ^ Salt
6161 -> Int -- ^ Iterations
@@ -65,8 +65,10 @@ pbkdf2 h s i = unsafeCoerce {name: "PBKDF2", hash: h, salt: s, iterations: i}
6565
6666
6767
68+ -- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams
6869hmac :: HashingFunction -> DeriveTargetAlgorithm
6970hmac h = unsafeCoerce {name: " HMAC" , hash: h}
7071
72+ -- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams
7173aes :: AESAlgorithm -> AESBitLength -> DeriveTargetAlgorithm
7274aes a l = unsafeCoerce {name: a, length: l}
0 commit comments