Skip to content

Commit 5861aa7

Browse files
committed
Upgrade to PureScript 0.15
1 parent d442f77 commit 5861aa7

18 files changed

+155
-143
lines changed

packages.dhall

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,9 @@ let additions =
116116
}
117117
-------------------------------
118118
-}
119-
120-
121119
let 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

124123
let overrides = {=}
125124

spago.dhall

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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')
77
import 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)
149
import 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)
1714
import Unsafe.Coerce (unsafeCoerce)
1815

1916

2017

2118
foreign import data EncryptAlgorithm :: Type
2219

20+
-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaOaepParams
2321
rsaOAEP :: Maybe ArrayBuffer -- ^ Label
2422
-> EncryptAlgorithm
2523
rsaOAEP 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
2928
aesCTR :: ArrayBuffer -- ^ Counter
3029
-> Int -- ^ Counter length
3130
-> EncryptAlgorithm
3231
aesCTR c l = unsafeCoerce {name: "AES-CTR", counter: c, length: l}
3332

33+
-- | https://developer.mozilla.org/en-US/docs/Web/API/AesCbcParams
3434
aesCBC :: ArrayBuffer -- ^ Initialization vector
3535
-> EncryptAlgorithm
3636
aesCBC i = unsafeCoerce {name: "AES-CBC", iv: i}
3737

38+
-- | https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
3839
aesGCM :: ArrayBuffer -- ^ Initialization vector
3940
-> Maybe ArrayBuffer -- ^ Additional data
4041
-> Maybe AESTagLength -- ^ Tag length
@@ -53,20 +54,20 @@ aesKW = unsafeCoerce {name: "AES-KW"}
5354

5455
foreign import encryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer)
5556

57+
-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
5658
encrypt :: 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

6566
foreign import decryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer)
6667

68+
-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
6769
decrypt :: 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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module 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)
45
import Data.ArrayBuffer.Types (ArrayBuffer)
56
import 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"
2525
sha512 :: HashingFunction
2626
sha512 = HashingFunction "SHA-512"
2727

28-
2928
foreign import digestImpl :: Fn2 HashingFunction ArrayBuffer (Promise ArrayBuffer)
3029

31-
30+
-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
3231
digest :: 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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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')
119
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)
1513
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)
1916
import Unsafe.Coerce (unsafeCoerce)
2017

2118

@@ -24,38 +21,41 @@ foreign import deriveKeyImpl :: Fn5 DeriveAlgorithm CryptoKey DeriveTargetAlgori
2421
foreign import deriveBitsImpl :: Fn3 DeriveAlgorithm CryptoKey Int (Promise ArrayBuffer)
2522

2623

24+
-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey
2725
deriveKey :: 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
3634
deriveBits :: 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

4441
foreign import data DeriveAlgorithm :: Type
4542
foreign import data DeriveTargetAlgorithm :: Type
4643

4744

45+
-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#ecdh
4846
ec :: ECAlgorithm
4947
-> CryptoKey -- ^ Public key of the other entity
5048
-> DeriveAlgorithm
5149
ec e k = unsafeCoerce {name: e, public: k}
5250

51+
-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#hkdf
5352
hkdf :: HashingFunction
5453
-> ArrayBuffer -- ^ Salt
5554
-> ArrayBuffer -- ^ Info
5655
-> DeriveAlgorithm
5756
hkdf 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
5959
pbkdf2 :: 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
6869
hmac :: HashingFunction -> DeriveTargetAlgorithm
6970
hmac h = unsafeCoerce {name: "HMAC", hash: h}
7071

72+
-- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams
7173
aes :: AESAlgorithm -> AESBitLength -> DeriveTargetAlgorithm
7274
aes a l = unsafeCoerce {name: a, length: l}

src/Crypto/Subtle/Key/Generate.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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')
129
import 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)
1614
import 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)
2017
import 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
2827
generateKey :: 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
3636
generateKeyPair :: 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

4443
foreign import data SymmetricAlgorithm :: Type
@@ -48,19 +47,23 @@ foreign import data AsymmetricAlgorithm :: Type
4847
foreign import exp65537 :: Uint8Array
4948

5049

50+
-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaHashedKeyGenParams
5151
rsa :: RSAAlgorithm
5252
-> Int -- ^ Modulus length. Should be at least 2048, or 4096 according to some bozo
5353
-> Uint8Array -- ^ Public exponent. Just use `exp65537`.
5454
-> HashingFunction
5555
-> AsymmetricAlgorithm
5656
rsa 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
5859
ec :: ECAlgorithm -> ECCurve -> AsymmetricAlgorithm
5960
ec e c = unsafeCoerce {name: e, namedCurve: c}
6061

62+
-- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams
6163
hmac :: HashingFunction -> SymmetricAlgorithm
6264
hmac h = unsafeCoerce {name: "HMAC", hash: h}
6365

66+
-- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams
6467
aes :: AESAlgorithm
6568
-> AESBitLength
6669
-> SymmetricAlgorithm

0 commit comments

Comments
 (0)