/
Cipher.purs
87 lines (57 loc) · 1.32 KB
/
Cipher.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module Node.Crypto.Cipher
( Cipher
, Algorithm(..)
, Password
, hex
, base64
, createCipher
, update
, final
) where
import Prelude
import Effect (Effect)
import Node.Encoding (Encoding(UTF8, Hex, Base64))
import Node.Buffer (Buffer, fromString, toString, concat)
foreign import data Cipher :: Type
data Algorithm
= AES128
| AES192
| AES256
type Password = String
instance showAlgorithm :: Show Algorithm where
show AES128 = "aes128"
show AES192 = "aes192"
show AES256 = "aes256"
hex
:: Algorithm
-> Password
-> String
-> Effect String
hex alg password str = cipher alg password str Hex
base64
:: Algorithm
-> Password
-> String
-> Effect String
base64 alg password str = cipher alg password str Base64
cipher
:: Algorithm
-> Password
-> String
-> Encoding
-> Effect String
cipher alg password str enc = do
buf <- fromString str UTF8
cip <- createCipher alg password
rbuf1 <- update cip buf
rbuf2 <- final cip
rbuf <- concat [ rbuf1, rbuf2 ]
toString enc rbuf
createCipher :: Algorithm -> Password -> Effect Cipher
createCipher alg password = _createCipher (show alg) password
foreign import _createCipher
:: String
-> String
-> Effect Cipher
foreign import update :: Cipher -> Buffer -> Effect Buffer
foreign import final :: Cipher -> Effect Buffer