UNPKG

3.79 kBtext/coffeescriptView Raw
1assert = require 'assert'
2Crypto = require '../src/crypto'
3
4describe 'Crypto', ->
5
6 ###*
7 * ENCRYPTION
8 ###
9
10 encoding = 'hex'
11 plaintext = new Buffer('0123456789ABCDEF')
12 key = "f0fb2c2dea3b28bd08a60c7049a5f62c3388872b82160a005ad3eb00ff0f69f3"
13 iv = "6993260ef345b11c7c53e4dac76326f9"
14 ciphertext = '489aa87997c0b113e5000111c32a79cd'
15
16 it 'should encrypt', ->
17 encrypted = Crypto.encrypt(plaintext, key, iv, encoding)
18 assert.equal encrypted, ciphertext
19
20 it 'should decrypt', ->
21 decrypted = Crypto.decrypt(ciphertext, key, iv)
22 assert.equal decrypted.toString(), plaintext.toString()
23
24
25 ###*
26 * PBKDF2
27 ###
28
29 it 'should pbkdf2', ->
30 password = 'password'
31 salt = '0123456789ABCDEF'
32 pbkdf2 = Crypto.pbkdf2(password, salt, 10000, 512)
33 assert.equal pbkdf2, 'f119d9566f66c185d1616fc88d7edcb003abd7bcef4e1dcf3fbc628cb9acface5fdda2f14320661feddf6ebda3e10f313ba7c2a12e532050668d229d67f9f6a0'
34
35
36 ###*
37 * HMAC
38 ###
39
40 it 'should hmac', ->
41 hash = Crypto.hmac(
42 'bdd50cd25aacbab410ce7b8b9dcb97b17340be26793632fc80983d3bf525e0d7',
43 '1bb1cc1d4f43b0a632ffe2ab1520f44df989ad33b2d82635d600aae8e05f5b4e',
44 256
45 )
46 assert.equal hash, '83f6ba83b23b2efa853a9d695fb5e349d4ea65360478fec345ee14b70c5f4699'
47
48 ###*
49 * HASH
50 ###
51
52 it 'should hash', ->
53 hash = Crypto.hash('f04a1b185b1bfb92c73ecd430642b59332190b94e7ea0b5a9c65490032dcf253', 256)
54 assert.equal hash, 'bf1b0a9672b18dd90790fbe044155b1750e978fa832237ff4fb615c5e235034f'
55
56
57 ###*
58 * Padding
59 ###
60
61 it 'should pad', ->
62 input = new Buffer('000000', 'hex')
63 padding = Crypto.pad(input)
64 assert.equal padding.length, 16
65 assert.equal padding.toString('hex')[-6..], '000000'
66
67 it 'should unpad', ->
68 input = new Buffer('00000000000000001122334455667788', 'hex')
69 unpadded = Crypto.unpad(8, input)
70 assert.equal unpadded.length, 16
71 assert.equal unpadded.toString('hex'), '1122334455667788'
72
73
74 ###*
75 * Random Data
76 ###
77
78 it 'should generate random data', ->
79 bytes = Crypto.randomBytes(64)
80 assert.equal bytes.length, 64
81
82
83 ###*
84 * Encoding
85 ###
86
87 it 'should convert data to a Buffer', ->
88 data = Crypto.randomBytes(64)
89 hex = data.toString('hex')
90 buffer = Crypto.toBuffer(hex, 'hex')
91 assert.equal (buffer instanceof Buffer), true
92
93 it 'should convert data to Hex', ->
94 data = Crypto.randomBytes(64)
95 hex = Crypto.toHex(data)
96 assert.equal data.toString('hex'), hex
97
98 it 'should convert data from Base64', ->
99 data = Crypto.randomBytes(64)
100 buffer = Crypto.fromBase64 data.toString('base64')
101 assert.equal buffer.toString('hex'), data.toString('hex')
102
103 it 'should concat buffers', ->
104 a = Crypto.randomBytes(16)
105 b = Crypto.randomBytes(16)
106 c = Crypto.randomBytes(16)
107 d = Crypto.concat([a, b, c])
108 assert.equal d.length, 48
109 assert.equal d.slice(0, 16).toString(), a.toString()
110 assert.equal d.slice(16, 32).toString(), b.toString()
111 assert.equal d.slice(32, 48).toString(), c.toString()
112
113 it 'should read and write little endian numbers', ->
114 numbers =
115 8192: "0020000000000000"
116 256: "0001000000000000"
117 64: "4000000000000000"
118 for val, endian of numbers
119 assert.equal Crypto.parseLittleEndian(endian), val
120 assert.equal Crypto.stringifyLittleEndian(val), endian
121
122 it 'should convert decimal to hex', ->
123 numbers =
124 0: "00"
125 60: "3c"
126 128: "80"
127 160: "a0"
128 255: "ff"
129 for dec, hex of numbers
130 dec = parseInt(dec, 10)
131 assert.equal Crypto.dec2hex(dec), hex
132
133 it 'should convert binary to hex', ->
134 hex = 'bada55c0ffee'
135 binary = 'ºÚUÀÿî'
136 assert.equal Crypto.bin2hex(binary), hex
137
138
139 it 'should generate UUIDS', ->
140 uuid = Crypto.generateUuid()
141 assert.equal uuid.length, 32