forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.spec.js
More file actions
147 lines (126 loc) · 5.83 KB
/
encryption.spec.js
File metadata and controls
147 lines (126 loc) · 5.83 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
/**
* adonis-framework
* Copyright(c) 2015-2016 Harminder Virk
* MIT Licensed
*/
const Encryption = require('../../src/Encryption')
const chai = require('chai')
const crypto = require('crypto')
const expect = chai.expect
let config
let encryption
class Config {
constructor (key, algorithm) {
this.key = key
this.algorithm = algorithm || 'aes-256-cbc'
}
get (key) {
if (key === 'app.appKey') {
return this.key
}
if (key === 'app.encryption.algorithm') {
return this.algorithm
}
}
}
describe('Encryption', function () {
before(function () {
config = new Config('a'.repeat(32), 'aes-256-cbc')
encryption = new Encryption(config)
})
it('should throw error when APP_KEY is not defined', function () {
const fn = function () {
return new Encryption(new Config())
}
expect(fn).to.throw('RuntimeException: E_MISSING_APPKEY: App key needs to be specified in order to make use of Encryption')
})
it('should throw error when APP_KEY to long', function () {
const fn = function () {
return new Encryption(new Config('a'.repeat(32), 'aes-128-cbc'))
}
expect(fn).to.throw('RuntimeException: E_INVALID_ENCRPYTION_CIPHER: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths')
})
it('should throw error when APP_KEY is wrong', function () {
const fn = function () {
return new Encryption(new Config('a'.repeat(5), 'aes-256-cbc'))
}
expect(fn).to.throw('RuntimeException: E_INVALID_ENCRPYTION_CIPHER: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths')
})
it('should throw error when cipher is unsupported', function () {
const fn = function () {
return new Encryption(new Config('a'.repeat(16), 'AES-256-CFB8'))
}
expect(fn).to.throw('RuntimeException: E_INVALID_ENCRPYTION_CIPHER: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths')
})
it('should throw error when APP_KEY length is wrong and cipher is unsupported', function () {
const fn = function () {
return new Encryption(new Config('a'.repeat(16), 'AES-256-CFB8'))
}
expect(fn).to.throw('RuntimeException: E_INVALID_ENCRPYTION_CIPHER: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths')
})
it('should calculate a correct sha256 hash', function () {
const hash = encryption.hash('These Aren\'t the Droids ', 'You\'re Looking For')
expect(hash).to.equal(crypto.createHmac('sha256', config.get('app.appKey')).update('These Aren\'t the Droids You\'re Looking For').digest('hex'))
})
it('should calculate a correct sha256 hash using HMAC method', function () {
const hmac = encryption.hashHmac('sha256', 'These Aren\'t the Droids You\'re Looking For', config.get('app.appKey'))
expect(hmac).to.equal(crypto.createHmac('sha256', config.get('app.appKey')).update('These Aren\'t the Droids You\'re Looking For').digest('hex'))
})
it('should encode base64', function () {
const base64 = encryption.base64Encode('These Aren\'t the Droids You\'re Looking For')
expect(base64).to.equal('VGhlc2UgQXJlbid0IHRoZSBEcm9pZHMgWW91J3JlIExvb2tpbmcgRm9y')
})
it('should decode base64', function () {
const plain = encryption.base64Decode('VGhlc2UgQXJlbid0IHRoZSBEcm9pZHMgWW91J3JlIExvb2tpbmcgRm9y')
expect(plain).to.equal('These Aren\'t the Droids You\'re Looking For')
})
it('should detect valid payload', function () {
const invalid = encryption.invalidPayload({iv: '', value: '', mac: ''})
expect(invalid).to.equal(false)
})
it('should detect valid mac', function () {
const payload = {iv: 'gD+wK78S1q4L3Vzgullp8Q==', value: 'These Aren\'t the Droids You\'re Looking For', mac: 'ffcfa6ced2727ba646467688e1f3ae0d38ccb7c5b4a9c6f9876d6d749100c2bd'}
const invalid = encryption.validMac(payload)
expect(invalid).to.equal(true)
})
it('should throw error when payload is invalid', function () {
const fn = function () {
return encryption.getJsonPayload('Int9Ig==')
}
expect(fn).to.throw('RuntimeException: E_INVALID_ENCRYPTION_PAYLOAD: The payload is invalid')
})
it('should throw error when payload is not an json object', function () {
const fn = function () {
return encryption.getJsonPayload('foo')
}
expect(fn).to.throw('RuntimeException: E_MALFORMED_JSON: The payload is not a json object')
})
it('should throw error when mac is invalid', function () {
let iv = crypto.randomBytes(16)
const mac = encryption.hash(iv = encryption.base64Encode(iv), 'These Aren\'t the Droids You\'re Looking For')
const json = JSON.stringify({iv: iv, value: 'These Are the Droids You\'re Looking For', mac: mac})
const base64 = encryption.base64Encode(json)
const fn = function () {
return encryption.getJsonPayload(base64)
}
expect(fn).to.throw('RuntimeException: E_INVALID_ENCRYPTION_MAC: The MAC is invalid')
})
it('should throw error when encrypt value is empty', function () {
const fn = () => encryption.encrypt('')
expect(fn).to.throw('InvalidArgumentException: E_MISSING_PARAMETER: Could not encrypt the data')
})
it('should decrypt values using defined algorithm', function () {
const encrypted = encryption.encrypt('These Aren\'t the Droids You\'re Looking For')
const decrypted = encryption.decrypt(encrypted)
expect(decrypted).to.equal('These Aren\'t the Droids You\'re Looking For')
})
it('should throw error with different keys', function () {
const fn = function () {
const a = new Encryption(new Config('a'.repeat(32), 'aes-256-cbc'))
const b = new Encryption(new Config('b'.repeat(32), 'aes-256-cbc'))
console.log(b.decrypt(a.encrypt('These Aren\'t the Droids You\'re Looking For')))
}
expect(fn).to.throw('RuntimeException: E_INVALID_ENCRYPTION_MAC: The MAC is invalid')
})
})