forked from dekz/dcrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcrypt.coffee
124 lines (103 loc) · 4.17 KB
/
dcrypt.coffee
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
_bindings = require './build/default/dcrypt'
#OpenSSL bindings for Node.js. This module extends what is in node-crypto bringing much needed additional functionality.
Random = _bindings.Random
Hash = _bindings.Hash
Sign = _bindings.Sign
Verify = _bindings.Verify
KeyPair = _bindings.KeyPair
Encode = _bindings.Encode
Cipher = _bindings.Cipher
Decipher = _bindings.Decipher
Rsa = _bindings.Rsa
Hmac = _bindings.Hmac
X509 = _bindings.X509
dcrypt = {}
##Random
#Given a length, randomBytes returns a buffer full of random data.
dcrypt.random = {}
dcrypt.random.randomBytes = (len) ->
len = len or 16
buff = new Buffer len
rb = new Random()
rb.randomBytes buff
return buff
exports.random = dcrypt.random
##Hash
#Create a hash object to digest some messages. Call hash.update(msg) to continually digest new data and hash.final(encoding_type) to receive the final digested message.
dcrypt.hash = Hash
exports.hash = {}
exports.hash.createHash = (hash) ->
return new Hash hash
##Sign
#Sign a message with a private key. This message can then be verified as coming from that private key with a corresponding public key. This object can take and PKI algorithm type, such as ECDSA and RSA.
dcrypt.sign = Sign
exports.sign = {}
exports.sign.createSign = (algo) ->
return (new Sign).init algo
##Verify
#Use verify to verify a signed message came from the associated private key. Verify.final can take either a PEM encoded certificate or a PEM encoded public key. Result of Verify.final is a boolean as to whether it passed or not.
dcrypt.verify = Verify
exports.verify= {}
exports.verify.createVerify= (algo) ->
return (new Verify).init algo
##Key Pairs
dcrypt.keypair = KeyPair
exports.keypair = {}
# newRSA takes a size in bytes of the key, as well as a exponent size. Both these arguments are optional and default to 1024 for key and 65537 for exponent by default
exports.keypair.newRSA = (size, exp) ->
size = size || 1024
exp = exp || 65537
return (new KeyPair).newRSA(size, exp)
# newECDSA creates a new ecdsa key pair, if no curve is supplied it uses "secp256k1" as default
exports.keypair.newECDSA = (curve) ->
curve = curve or "secp256k1"
return (new KeyPair).newECDSA(curve)
# parseECDSA parses the raw ECDSA parameters from a PEM file's contents
exports.keypair.parseECDSA = (filename, public) ->
return (new KeyPair).parseECDSA(filename, public)
# parseRSA parses the raw RSA parameters from a PEM file's contents
exports.keypair.parseRSA = (filename, public) ->
return (new KeyPair).parseRSA(filename, public)
dcrypt.encode = Encode
exports.encode = {}
exports.encode.encodeBase58 = (data) ->
return (new Encode).encodeBase58(data)
exports.encode.decodeBase58 = (data) ->
return (new Encode).decodeBase58(data)
##Cipher
dcrypt.cipher = Cipher
exports.cipher = {}
exports.cipher.createCipher = (cipher, key) ->
return (new Cipher).init(cipher, key)
exports.cipher.createCipheriv = (cipher, key, iv) ->
return (new Cipher).initiv(cipher, key, iv)
dcrypt.decipher = Decipher
exports.decipher = {}
exports.decipher.createDecipher = (cipher, key) ->
return (new Decipher).init(cipher, key)
exports.cipher.createDecipheriv = (cipher, key, iv) ->
return (new Decipher).initiv(cipher, key, iv)
##RSA
#Encrypt a message with a RSA public key and decrypt it with the associated private key. Public key can either be in PEM key format or a PEM certificate. If no padding is chosen PKCS1 padding will be used. If no output encoding is supplied, hex is used.
dcrypt.rsa = Rsa
exports.rsa = {}
exports.rsa.encrypt = (pem_pub, msg, padding, out_encoding) ->
out_encoding = out_encoding or 'hex'
padding = padding or 'RSA_PKCS1_PADDING'
return (new Rsa).encrypt(pem_pub, msg, padding, out_encoding)
exports.rsa.decrypt = (pem_priv, enc_msg, padding, in_encoding) ->
out_encoding = out_encoding or 'hex'
padding = padding or 'RSA_PKCS1_PADDING'
return (new Rsa).decrypt(pem_priv, enc_msg, padding, in_encoding)
##HMAC
dcrypt.hmac = Hmac
exports.hmac = {}
exports.hmac.createHmac = (hmac, key) ->
return (new Hmac).init(hmac, key)
##X509
dcrypt.x509 = X509
exports.x509 = {}
exports.x509.parse = (cert) ->
return (new X509).parse(cert)
exports.x509.createCert = (args) ->
return (new X509).createCert(args)