git.iko.soy/rowtype-yoga/purescript-yoga-subtlecrypto
on master help sign in
Upgrade to PureScript 0.15
commit7999dc92b94d1b7285375cfaad4a0ae6644b71b6 authorJames Brock <jamesbrock@gmail.com> dateWed 18 May 2022, 15:42 (4 years ago) parentd442f77
18 files changed+154−143
packages.dhall+2−3 spago.dhall+8−2 src/Crypto/Subtle/Encrypt.js+2−4 src/Crypto/Subtle/Encrypt.purs+15−14 src/Crypto/Subtle/Hash.js+1−3 src/Crypto/Subtle/Hash.purs+6−9 src/Crypto/Subtle/Key/Derive.js+2−4 src/Crypto/Subtle/Key/Derive.purs+15−13 src/Crypto/Subtle/Key/Generate.js+2−4 src/Crypto/Subtle/Key/Generate.purs+17−14 src/Crypto/Subtle/Key/Import.js+1−3 src/Crypto/Subtle/Key/Import.purs+9−14 src/Crypto/Subtle/Key/Types.js+1−3 src/Crypto/Subtle/Key/Types.purs+46−18 src/Crypto/Subtle/Key/Wrap.js+2−4 src/Crypto/Subtle/Key/Wrap.purs+11−14 src/Crypto/Subtle/Sign.js+4−6 src/Crypto/Subtle/Sign.purs+10−11
packages.dhall+2−3
@@ -116,10 +116,9 @@ let additions =
116116 }
117117-------------------------------
118118-}
119−
120−
121119let upstream =
122− https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200423/packages.dhall sha256:c180a06bb5444fd950f8cbdd6605c644fd246deb397e62572b8f4a6b9dbcaf22
120+ https://github.com/purescript/package-sets/releases/download/psc-0.15.0-20220516/packages.dhall
121+ sha256:b0bf932de16a10b7d69c6bbbb31ec9ca575237c43a999fa32e59e35eb8c024a1
123122
124123let overrides = {=}
125124
spago.dhall+8−2
@@ -5,13 +5,19 @@ You can edit this file as you like.
55{ name = "subtlecrypto"
66, dependencies =
77 [ "aff"
8+ , "aff-promise"
89 , "arraybuffer-types"
910 , "console"
1011 , "effect"
12+ , "either"
13+ , "exceptions"
1114 , "foreign"
15+ , "functions"
16+ , "maybe"
1217 , "prelude"
13− , "promises"
14− , "psci-support"
18+ , "transformers"
19+ , "tuples"
20+ , "unsafe-coerce"
1521 ]
1622, packages = ./packages.dhall
1723, sources = [ "src/**/*.purs", "test/**/*.purs" ]
src/Crypto/Subtle/Encrypt.js+2−4
@@ -1,8 +1,6 @@
1−"use strict";
2−
3−exports.encryptImpl = function encryptImpl (a,k,x) {
1+export function encryptImpl (a,k,x) {
42 return crypto.subtle.encrypt(a,k,x);
53};
6−exports.decryptImpl = function decryptImpl (a,k,x) {
4+export function decryptImpl (a,k,x) {
75 return crypto.subtle.decrypt(a,k,x);
86};
src/Crypto/Subtle/Encrypt.purs+15−14
@@ -3,38 +3,39 @@ module Crypto.Subtle.Encrypt
33 , EncryptAlgorithm, rsaOAEP, aesCTR, aesCBC, aesGCM, aesKW
44 ) where
55
6−import Crypto.Subtle.Key.Types (CryptoKey)
6+import Control.Promise (Promise, toAff')
77import Crypto.Subtle.Constants.AES (AESTagLength)
8−
9−import Prelude ((<<<), (<$))
10−import Data.Function.Uncurried (Fn3, runFn3)
11−import Data.Tuple (Tuple (..))
12−import Data.Maybe (Maybe (..))
13−import Data.Either (Either (..))
8+import Crypto.Subtle.Key.Types (CryptoKey, errorFromDOMException)
149import Data.ArrayBuffer.Types (ArrayBuffer)
15−import Effect.Promise (Promise, runPromise)
16−import Effect.Aff (Aff, makeAff, nonCanceler)
10+import Data.Function.Uncurried (Fn3, runFn3)
11+import Data.Maybe (Maybe(..))
12+import Data.Tuple (Tuple(..))
13+import Effect.Aff (Aff)
1714import Unsafe.Coerce (unsafeCoerce)
1815
1916
2017
2118foreign import data EncryptAlgorithm :: Type
2219
20+-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaOaepParams
2321rsaOAEP :: Maybe ArrayBuffer -- ^ Label
2422 -> EncryptAlgorithm
2523rsaOAEP mL = case mL of
2624 Nothing -> unsafeCoerce {name: "RSA_OAEP"}
2725 Just l -> unsafeCoerce {name: "RSA_OAEP", label: l}
2826
27+-- | https://developer.mozilla.org/en-US/docs/Web/API/AesCtrParams
2928aesCTR :: ArrayBuffer -- ^ Counter
3029 -> Int -- ^ Counter length
3130 -> EncryptAlgorithm
3231aesCTR c l = unsafeCoerce {name: "AES-CTR", counter: c, length: l}
3332
33+-- | https://developer.mozilla.org/en-US/docs/Web/API/AesCbcParams
3434aesCBC :: ArrayBuffer -- ^ Initialization vector
3535 -> EncryptAlgorithm
3636aesCBC i = unsafeCoerce {name: "AES-CBC", iv: i}
3737
38+-- | https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
3839aesGCM :: ArrayBuffer -- ^ Initialization vector
3940 -> Maybe ArrayBuffer -- ^ Additional data
4041 -> Maybe AESTagLength -- ^ Tag length
@@ -53,20 +54,20 @@ aesKW = unsafeCoerce {name: "AES-KW"}
5354
5455foreign import encryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer)
5556
57+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
5658encrypt :: EncryptAlgorithm
5759 -> CryptoKey
5860 -> ArrayBuffer
5961 -> Aff ArrayBuffer
60−encrypt a k x = makeAff \resolve ->
61− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 encryptImpl a k x)
62+encrypt a k x = toAff' errorFromDOMException (runFn3 encryptImpl a k x)
6263
6364
6465
6566foreign import decryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer)
6667
68+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
6769decrypt :: EncryptAlgorithm
6870 -> CryptoKey
6971 -> ArrayBuffer
70− -> Aff (Maybe ArrayBuffer)
71−decrypt a k x = makeAff \resolve ->
72− nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn3 decryptImpl a k x)
72+ -> Aff ArrayBuffer
73+decrypt a k x = toAff' errorFromDOMException (runFn3 decryptImpl a k x)
src/Crypto/Subtle/Hash.js+1−3
@@ -1,5 +1,3 @@
1−"use strict";
2−
3−exports.digestImpl = function digestImpl (h,x) {
1+export function digestImpl (h,x) {
42 return crypto.subtle.digest(h,x);
53};
src/Crypto/Subtle/Hash.purs+6−9
@@ -1,11 +1,11 @@
11module Crypto.Subtle.Hash (HashingFunction, sha1, sha256, sha384, sha512, digest) where
22
3−import Prelude ((<<<), (<$), class Eq)
3+import Control.Promise (Promise, toAff')
4+import Crypto.Subtle.Key.Types (errorFromDOMException)
45import Data.ArrayBuffer.Types (ArrayBuffer)
56import Data.Function.Uncurried (Fn2, runFn2)
6−import Data.Either (Either (..))
7−import Effect.Promise (Promise, runPromise)
8−import Effect.Aff (Aff, makeAff, nonCanceler)
7+import Effect.Aff (Aff)
8+import Prelude (class Eq)
99
1010
1111
@@ -25,11 +25,8 @@ sha384 = HashingFunction "SHA-384"
2525sha512 :: HashingFunction
2626sha512 = HashingFunction "SHA-512"
2727
28−
2928foreign import digestImpl :: Fn2 HashingFunction ArrayBuffer (Promise ArrayBuffer)
3029
31−
30+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
3231digest :: HashingFunction -> ArrayBuffer -> Aff ArrayBuffer
33−digest h x =
34− let p = runFn2 digestImpl h x
35− in makeAff \resolve -> nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) p
32+digest h x = toAff' errorFromDOMException (runFn2 digestImpl h x)
src/Crypto/Subtle/Key/Derive.js+2−4
@@ -1,8 +1,6 @@
1−"use strict";
2−
3−exports.deriveKeyImpl = function deriveKeyImpl (a,k,t,e,u) {
1+export function deriveKeyImpl (a,k,t,e,u) {
42 return crypto.subtle.deriveKey(a,k,t,e,u);
53};
6−exports.deriveBitsImpl = function deriveBitsImpl (a,k,l) {
4+export function deriveBitsImpl (a,k,l) {
75 return crypto.subtle.deriveBits(a,k,l);
86};
src/Crypto/Subtle/Key/Derive.purs+15−13
@@ -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}
src/Crypto/Subtle/Key/Generate.js+2−4
@@ -1,6 +1,4 @@
1−"use strict";
2−
3−exports.generateKeyImpl = function generateKeyImpl (a,e,u) {
1+export function generateKeyImpl (a,e,u) {
42 return crypto.subtle.generateKey(a,e,u);
53};
6−exports.exp65537 = new Uint8Array([0x01,0x00,0x01]);
4+export const exp65537 = new Uint8Array([0x01,0x00,0x01]);
src/Crypto/Subtle/Key/Generate.purs+17−14
@@ -5,18 +5,15 @@ module Crypto.Subtle.Key.Generate
55 , exp65537
66 ) where
77
8−import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyPair, CryptoKeyUsage)
9−import Crypto.Subtle.Hash (HashingFunction)
10−import Crypto.Subtle.Constants.RSA (RSAAlgorithm)
11−import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve)
8+import Control.Promise (Promise, toAff')
129import Crypto.Subtle.Constants.AES (AESAlgorithm, AESBitLength)
13−
14−import Prelude ((<<<), (<$))
15−import Data.Function.Uncurried (Fn3, runFn3)
10+import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve)
11+import Crypto.Subtle.Constants.RSA (RSAAlgorithm)
12+import Crypto.Subtle.Hash (HashingFunction)
13+import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyPair, CryptoKeyUsage, errorFromDOMException)
1614import Data.ArrayBuffer.Types (Uint8Array)
17−import Data.Either (Either (..))
18−import Effect.Promise (Promise, runPromise)
19−import Effect.Aff (Aff, makeAff, nonCanceler)
15+import Data.Function.Uncurried (Fn3, runFn3)
16+import Effect.Aff (Aff)
2017import Unsafe.Coerce (unsafeCoerce)
2118
2219
@@ -25,20 +22,22 @@ foreign import generateKeyImpl :: forall a b. Fn3 a Boolean (Array CryptoKeyUsag
2522
2623
2724-- | Generate a symmetric key
25+-- |
26+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
2827generateKey :: SymmetricAlgorithm
2928 -> Boolean -- ^ Extractable
3029 -> Array CryptoKeyUsage
3130 -> Aff CryptoKey
32−generateKey a e u = makeAff \resolve ->
33− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 generateKeyImpl a e u)
31+generateKey a e u = toAff' errorFromDOMException (runFn3 generateKeyImpl a e u)
3432
3533-- | Generate an asymmetric keypair
34+-- |
35+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
3636generateKeyPair :: AsymmetricAlgorithm
3737 -> Boolean -- ^ Extractable
3838 -> Array CryptoKeyUsage
3939 -> Aff CryptoKeyPair
40−generateKeyPair a e u = makeAff \resolve ->
41− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 generateKeyImpl a e u)
40+generateKeyPair a e u = toAff' errorFromDOMException (runFn3 generateKeyImpl a e u)
4241
4342
4443foreign import data SymmetricAlgorithm :: Type
@@ -48,6 +47,7 @@ foreign import data AsymmetricAlgorithm :: Type
4847foreign import exp65537 :: Uint8Array
4948
5049
50+-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaHashedKeyGenParams
5151rsa :: RSAAlgorithm
5252 -> Int -- ^ Modulus length. Should be at least 2048, or 4096 according to some bozo
5353 -> Uint8Array -- ^ Public exponent. Just use `exp65537`.
@@ -55,12 +55,15 @@ rsa :: RSAAlgorithm
5555 -> AsymmetricAlgorithm
5656rsa r l e h = unsafeCoerce {name: r, modulusLength: l, publicExponent: e, hash: h}
5757
58+-- | https://developer.mozilla.org/en-US/docs/Web/API/EcKeyGenParams
5859ec :: ECAlgorithm -> ECCurve -> AsymmetricAlgorithm
5960ec e c = unsafeCoerce {name: e, namedCurve: c}
6061
62+-- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams
6163hmac :: HashingFunction -> SymmetricAlgorithm
6264hmac h = unsafeCoerce {name: "HMAC", hash: h}
6365
66+-- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams
6467aes :: AESAlgorithm
6568 -> AESBitLength
6669 -> SymmetricAlgorithm
src/Crypto/Subtle/Key/Import.js+1−3
@@ -1,5 +1,3 @@
1−"use strict";
2−
3−exports.importKeyImpl = function importKeyImpl (f,x,a,e,u) {
1+export function importKeyImpl (f,x,a,e,u) {
42 return crypto.subtle.importKey(f,x,a,e,u);
53};
src/Crypto/Subtle/Key/Import.purs+9−14
@@ -3,18 +3,15 @@ module Crypto.Subtle.Key.Import
33 , ImportAlgorithm, rsa, ec, hmac, aes
44 ) where
55
6−import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat)
7−import Crypto.Subtle.Hash (HashingFunction)
8−import Crypto.Subtle.Constants.RSA (RSAAlgorithm)
9−import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve)
6+import Control.Promise (Promise, toAff')
107import Crypto.Subtle.Constants.AES (AESAlgorithm)
11−
12−import Prelude ((<<<), (<$))
13−import Data.Function.Uncurried (Fn5, runFn5)
8+import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve)
9+import Crypto.Subtle.Constants.RSA (RSAAlgorithm)
10+import Crypto.Subtle.Hash (HashingFunction)
11+import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat, errorFromDOMException)
1412import Data.ArrayBuffer.Types (ArrayBuffer)
15−import Data.Either (Either (..))
16−import Effect.Promise (Promise, runPromise)
17−import Effect.Aff (Aff, makeAff, nonCanceler)
13+import Data.Function.Uncurried (Fn5, runFn5)
14+import Effect.Aff (Aff)
1815import Unsafe.Coerce (unsafeCoerce)
1916
2017
@@ -22,16 +19,14 @@ import Unsafe.Coerce (unsafeCoerce)
2219foreign import importKeyImpl :: Fn5 ExternalFormat ArrayBuffer ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey)
2320
2421
22+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
2523importKey :: ExternalFormat
2624 -> ArrayBuffer -- ^ Key data
2725 -> ImportAlgorithm
2826 -> Boolean -- ^ Extractable
2927 -> Array CryptoKeyUsage
3028 -> Aff CryptoKey
31−importKey f x a e u = makeAff \resolve ->
32− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn5 importKeyImpl f x a e u)
33−
34−
29+importKey f x a e u = toAff' errorFromDOMException (runFn5 importKeyImpl f x a e u)
3530
3631
3732foreign import data ImportAlgorithm :: Type
src/Crypto/Subtle/Key/Types.js+1−3
@@ -1,5 +1,3 @@
1−"use strict";
2−
3−exports.exportKeyImpl = function exportKeyImpl (f,x) {
1+export function exportKeyImpl (f,x) {
42 return crypto.subtle.exportKey(f,x);
53};
src/Crypto/Subtle/Key/Types.purs+46−18
@@ -1,24 +1,43 @@
11module Crypto.Subtle.Key.Types
2− ( CryptoKeyType
3− , secret, public, private
2+ ( CryptoKey
3+ , CryptoKeyPair(..)
4+ , CryptoKeyType
45 , CryptoKeyUsage
5− , encrypt, decrypt, sign, verify, deriveKey, deriveBits, unwrapKey, wrapKey
6+ , ExternalFormat
67 , allUsages
7− , CryptoKey
8− , getType, getExtractable, getAlgorithm, getUsages
8+ , decrypt
9+ , deriveBits
10+ , deriveKey
11+ , encrypt
12+ , errorFromDOMException
913 , exportKey
10− , CryptoKeyPair (..)
11− , ExternalFormat, raw, pkcs8, spki, jwk
12− ) where
13−
14−
15−import Prelude ((<<<), (<$), class Eq)
16−import Data.Either (Either (..))
17−import Data.Function.Uncurried (Fn2, runFn2)
14+ , getAlgorithm
15+ , getExtractable
16+ , getType
17+ , getUsages
18+ , jwk
19+ , pkcs8
20+ , private
21+ , public
22+ , raw
23+ , secret
24+ , sign
25+ , spki
26+ , unwrapKey
27+ , verify
28+ , wrapKey
29+ )
30+ where
31+
32+import Prelude ((>>=), class Eq)
33+import Control.Monad.Except (runExcept)
34+import Control.Promise (Promise, toAff')
1835import Data.ArrayBuffer.Types (ArrayBuffer)
19−import Foreign (Foreign)
20−import Effect.Aff (Aff, makeAff, nonCanceler)
21−import Effect.Promise (Promise, runPromise)
36+import Data.Either (Either(..))
37+import Data.Function.Uncurried (Fn2, runFn2)
38+import Effect.Aff (Aff, Error, error)
39+import Foreign (Foreign, readString)
40+import Foreign.Index (readProp)
2241import Unsafe.Coerce (unsafeCoerce)
2342
2443
@@ -103,8 +122,17 @@ jwk = ExternalFormat "jwk"
103122
104123foreign import exportKeyImpl :: Fn2 ExternalFormat CryptoKey (Promise ArrayBuffer)
105124
125+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
106126exportKey :: ExternalFormat
107127 -> CryptoKey
108128 -> Aff ArrayBuffer
109−exportKey f x = makeAff \resolve ->
110− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn2 exportKeyImpl f x)
129+exportKey f x = toAff' errorFromDOMException (runFn2 exportKeyImpl f x)
130+
131+-- Most of the Promises throw DOMException, so read the message from the DOMException.
132+-- https://developer.mozilla.org/en-US/docs/Web/API/DOMException
133+-- Some of the Promises throw TypeError, which also has a message property.
134+-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError
135+errorFromDOMException :: Foreign -> Error
136+errorFromDOMException x = case runExcept (readProp "message" x >>= readString) of
137+ Left _ -> error "Not a DOMException"
138+ Right m -> error m
src/Crypto/Subtle/Key/Wrap.js+2−4
@@ -1,8 +1,6 @@
1−"use strict";
2−
3−exports.wrapKeyImpl = function wrapKeyImpl (f,x,k,a) {
1+export function wrapKeyImpl (f,x,k,a) {
42 return crypto.subtle.wrapKey(f,x,k,a);
53};
6−exports.unwrapKeyImpl = function unwrapKeyImpl (f,x,k,a,i,e,u) {
4+export function unwrapKeyImpl (f,x,k,a,i,e,u) {
75 return crypto.subtle.unwrapKey(f,x,k,a,i,e,u);
86};
src/Crypto/Subtle/Key/Wrap.purs+11−14
@@ -1,33 +1,30 @@
11module Crypto.Subtle.Key.Wrap
22 where
33
4−import Crypto.Subtle.Key.Types (CryptoKey, ExternalFormat, CryptoKeyUsage)
5−import Crypto.Subtle.Key.Import (ImportAlgorithm)
4+import Control.Promise (Promise, toAff')
65import Crypto.Subtle.Encrypt (EncryptAlgorithm)
7−
8−import Prelude ((<<<), (<$))
9−import Data.Function.Uncurried (Fn4, Fn7, runFn4, runFn7)
10−import Data.Maybe (Maybe (..))
11−import Data.Either (Either (..))
6+import Crypto.Subtle.Key.Import (ImportAlgorithm)
7+import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat, errorFromDOMException)
128import Data.ArrayBuffer.Types (ArrayBuffer)
13−import Effect.Promise (Promise, runPromise)
14−import Effect.Aff (Aff, makeAff, nonCanceler)
9+import Data.Function.Uncurried (Fn4, Fn7, runFn4, runFn7)
10+import Effect.Aff (Aff)
1511
1612
1713
1814foreign import wrapKeyImpl :: Fn4 ExternalFormat CryptoKey CryptoKey EncryptAlgorithm (Promise ArrayBuffer)
1915
16+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/wrapKey
2017wrapKey :: ExternalFormat
2118 -> CryptoKey -- ^ Subject key
2219 -> CryptoKey -- ^ Wrapping key
2320 -> EncryptAlgorithm
2421 -> Aff ArrayBuffer
25−wrapKey f x k a = makeAff \resolve ->
26− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn4 wrapKeyImpl f x k a)
22+wrapKey f x k a = toAff' errorFromDOMException (runFn4 wrapKeyImpl f x k a)
2723
2824
2925foreign import unwrapKeyImpl :: Fn7 ExternalFormat ArrayBuffer CryptoKey EncryptAlgorithm ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey)
3026
27+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/unwrapKey
3128unwrapKey :: ExternalFormat
3229 -> ArrayBuffer -- ^ Wrapped key
3330 -> CryptoKey -- ^ Unwrapping key
@@ -35,6 +32,6 @@ unwrapKey :: ExternalFormat
3532 -> ImportAlgorithm
3633 -> Boolean -- ^ Extractable
3734 -> Array CryptoKeyUsage
38− -> Aff (Maybe CryptoKey)
39−unwrapKey f x k a i e u = makeAff \resolve ->
40− nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn7 unwrapKeyImpl f x k a i e u)
35+ -- -> Aff (Maybe CryptoKey)
36+ -> Aff CryptoKey
37+unwrapKey f x k a i e u = toAff' errorFromDOMException (runFn7 unwrapKeyImpl f x k a i e u)
src/Crypto/Subtle/Sign.js+4−6
@@ -1,8 +1,6 @@
1−"use strict";
2−
3−exports.signImpl = function signImpl (a,k,x) {
1+export function signImpl (a,k,x) {
42 return crypto.subtle.sign(a,k,x);
53};
6−exports.verifyImpl = function verifyImpl (a,k,s,x) {
7− return crypto.subtle.verify(a,k,s,x);
8−};
4+export function verifyImpl(a, k, s, x) {
5+ return crypto.subtle.verify(a, k, s, x);
6+}
src/Crypto/Subtle/Sign.purs+10−11
@@ -3,15 +3,12 @@ module Crypto.Subtle.Sign
33 , SignAlgorithm, rsaPKCS1, rsaPSS, ecdsa, hmac
44 ) where
55
6−import Crypto.Subtle.Key.Types (CryptoKey)
6+import Control.Promise (Promise, toAff')
77import Crypto.Subtle.Hash (HashingFunction)
8−
9−import Prelude ((<<<), (<$))
10−import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
11−import Data.Either (Either (..))
8+import Crypto.Subtle.Key.Types (CryptoKey, errorFromDOMException)
129import Data.ArrayBuffer.Types (ArrayBuffer)
13−import Effect.Promise (Promise, runPromise)
14−import Effect.Aff (Aff, makeAff, nonCanceler)
10+import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
11+import Effect.Aff (Aff)
1512import Unsafe.Coerce (unsafeCoerce)
1613
1714
@@ -21,10 +18,12 @@ foreign import data SignAlgorithm :: Type
2118rsaPKCS1 :: SignAlgorithm
2219rsaPKCS1 = unsafeCoerce {name: "RSASSA-PKCS1-v1_5"}
2320
21+-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaPssParams
2422rsaPSS :: Int -- ^ Salt length
2523 -> SignAlgorithm
2624rsaPSS l = unsafeCoerce {name: "RSA-PSS", saltLength: l}
2725
26+-- | https://developer.mozilla.org/en-US/docs/Web/API/EcdsaParams
2827ecdsa :: HashingFunction
2928 -> SignAlgorithm
3029ecdsa h = unsafeCoerce {name: "ECDSA", hash: h}
@@ -35,20 +34,20 @@ hmac = unsafeCoerce {name: "HMAC"}
3534
3635foreign import signImpl :: Fn3 SignAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer)
3736
37+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign
3838sign :: SignAlgorithm
3939 -> CryptoKey
4040 -> ArrayBuffer
4141 -> Aff ArrayBuffer
42−sign a k x = makeAff \resolve ->
43− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 signImpl a k x)
42+sign a k x = toAff' errorFromDOMException (runFn3 signImpl a k x)
4443
4544
4645foreign import verifyImpl :: Fn4 SignAlgorithm CryptoKey ArrayBuffer ArrayBuffer (Promise Boolean)
4746
47+-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/verify
4848verify :: SignAlgorithm
4949 -> CryptoKey
5050 -> ArrayBuffer -- ^ Signature
5151 -> ArrayBuffer -- ^ Subject data
5252 -> Aff Boolean
53−verify a k s x = makeAff \resolve ->
54− nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn4 verifyImpl a k s x)
53+verify a k s x = toAff' errorFromDOMException (runFn4 verifyImpl a k s x)