/
Decipher.purs
56 lines (47 loc) · 1.19 KB
/
Decipher.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
module Node.Crypto.Decipher
( Decipher
, fromHex
, fromBase64
, createDecipher
, update
, final
) where
import Prelude
import Effect (Effect)
import Node.Encoding (Encoding(UTF8, Hex, Base64))
import Node.Buffer (Buffer, fromString, toString, concat)
import Node.Crypto.Cipher (Algorithm, Password)
foreign import data Decipher :: Type
fromHex
:: Algorithm
-> Password
-> String
-> Effect String
fromHex alg password str = decipher alg password str Hex
fromBase64
:: Algorithm
-> Password
-> String
-> Effect String
fromBase64 alg password str = decipher alg password str Base64
decipher
:: Algorithm
-> Password
-> String
-> Encoding
-> Effect String
decipher alg password str enc = do
buf <- fromString str enc
dec <- createDecipher alg password
rbuf1 <- update dec buf
rbuf2 <- final dec
rbuf <- concat [ rbuf1, rbuf2 ]
toString UTF8 rbuf
createDecipher :: Algorithm -> Password -> Effect Decipher
createDecipher alg password = _createDecipher (show alg) password
foreign import _createDecipher
:: String
-> String
-> Effect Decipher
foreign import update :: Decipher -> Buffer -> Effect Buffer
foreign import final :: Decipher -> Effect Buffer