git.iko.soy/rowtype-yoga/purescript-yoga-subtlecrypto
on master help sign in
import implementation
commit9eef81513abd1e900c222a62ba80911c427602bd authorAthan Clark <athan.clark@localcooking.com> dateWed 16 Jan 2019, 04:07 (8 years ago) parent31fff14
8 files changed+127−7
README.md+5 src/Crypto/Subtle/AES.purs+16 src/Crypto/Subtle/EC.purs+24 src/Crypto/Subtle/Hash.js+1−6 src/Crypto/Subtle/Key/Import.js+3 src/Crypto/Subtle/Key/Import.purs+63 src/Crypto/Subtle/Key.purs → src/Crypto/Subtle/Key/Types.purs+1−1 src/Crypto/Subtle/RSA.purs+14
README.mdnew file+5
@@ -0,0 +1,5 @@
1+# purescript-subtlecrypto
2+
3+Simple bindings to the Browser's `window.crypto.subtle` API thing.
4+
5+*__Warning__*: This only works on specific browsers - not even node.js!
src/Crypto/Subtle/AES.pursnew file+16
@@ -0,0 +1,16 @@
1+module Crypto.Subtle.AES
2+ ( AESAlgorithm, aesCTR, aesCBC, aesGCM, aesKW
3+ ) where
4+
5+
6+
7+newtype AESAlgorithm = AESAlgorithm String
8+
9+aesCTR :: AESAlgorithm
10+aesCTR = AESAlgorithm "AES-CTR"
11+aesCBC :: AESAlgorithm
12+aesCBC = AESAlgorithm "ES-CBC"
13+aesGCM :: AESAlgorithm
14+aesGCM = AESAlgorithm "AES-GCM"
15+aesKW :: AESAlgorithm
16+aesKW = AESAlgorithm "ES-KW"
src/Crypto/Subtle/EC.pursnew file+24
@@ -0,0 +1,24 @@
1+module Crypto.Subtle.EC
2+ ( ECAlgorithm, ecdsa, ecdh
3+ , ECCurve, p256, p384, p521
4+ ) where
5+
6+
7+
8+newtype ECAlgorithm = ECAlgorithm String
9+
10+ecdsa :: ECAlgorithm
11+ecdsa = ECAlgorithm "ECDSA"
12+ecdh :: ECAlgorithm
13+ecdh = ECAlgorithm "ECDH"
14+
15+
16+
17+newtype ECCurve = ECCurve String
18+
19+p256 :: ECCurve
20+p256 = ECCurve "P-256"
21+p384 :: ECCurve
22+p384 = ECCurve "P-384"
23+p521 :: ECCurve
24+p521 = ECCurve "P-521"
src/Crypto/Subtle/Hash.js+1−6
@@ -1,8 +1,3 @@
11"use strict";
22
3−var subtle = typeof module !== "undefined" && module.require
4− ? module.require('subtle')
5− : crypto.subtle;
6−
7−
8−exports.digestImpl = subtle.digest;
3+exports.digestImpl = crypto.subtle.digest;
src/Crypto/Subtle/Key/Import.jsnew file+3
@@ -0,0 +1,3 @@
1+"use strict";
2+
3+exports.importKeyImpl = crypto.subtle.importKey;
src/Crypto/Subtle/Key/Import.pursnew file+63
@@ -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}
src/Crypto/Subtle/Key.purs → src/Crypto/Subtle/Key/Types.pursrenamed+1−1
@@ -1,4 +1,4 @@
1−module Crypto.Subtle.Key
1+module Crypto.Subtle.Key.Types
22 ( CryptoKeyType
33 , CryptoKeyUsage
44 , encrypt, decrypt, sign, verify, deriveKey, deriveBits, unwrapKey, wrapKey
src/Crypto/Subtle/RSA.pursnew file+14
@@ -0,0 +1,14 @@
1+module Crypto.Subtle.RSA
2+ ( RSAAlgorithm, rsaPKCS1, rsaPSS, rsaOAEP
3+ ) where
4+
5+
6+
7+newtype RSAAlgorithm = RSAAlgorithm String
8+
9+rsaPKCS1 :: RSAAlgorithm
10+rsaPKCS1 = RSAAlgorithm "RSASSA-PKCS1-v1_5"
11+rsaPSS :: RSAAlgorithm
12+rsaPSS = RSAAlgorithm "RSA-PSS"
13+rsaOAEP :: RSAAlgorithm
14+rsaOAEP = RSAAlgorithm "RSA-OAEP"