-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Hash.js
179 lines (161 loc) · 4.37 KB
/
Hash.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import EnvUtil from '../EnvUtil'
import Encoder from '../Encoder'
import md5 from './Hash/md5'
import nodeCrypto from 'crypto'
const meta = {
name: 'hash',
title: 'Hash function',
category: 'Modern cryptography',
type: 'encoder'
}
const algorithms = [
{
name: 'md5',
label: 'MD5',
blockSize: 64,
available: true
},
{
name: 'sha1',
label: 'SHA-1',
blockSize: 64,
browserAlgorithm: 'SHA-1',
browserExceptions: ['ie=11', 'edge'],
nodeAlgorithm: 'sha1'
},
{
name: 'sha256',
label: 'SHA-256',
blockSize: 64,
browserAlgorithm: 'SHA-256',
nodeAlgorithm: 'sha256'
},
{
name: 'sha384',
label: 'SHA-384',
blockSize: 128,
browserAlgorithm: 'SHA-384',
nodeAlgorithm: 'sha384'
},
{
name: 'sha512',
label: 'SHA-512',
blockSize: 128,
browserAlgorithm: 'SHA-512',
browserExceptions: ['ie=11'],
nodeAlgorithm: 'sha512'
}
]
/**
* Encoder brick for creating message digests
*/
export default class HashEncoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.setEncodeOnly(true)
const algorithms = HashEncoder.filterAvailableAlgorithms()
this.addSetting({
name: 'algorithm',
type: 'enum',
value: 'sha256',
elements: algorithms.map(algorithm => algorithm.name),
labels: algorithms.map(algorithm => algorithm.label),
randomizable: false,
style: 'radio'
})
}
/**
* Performs encode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
*/
performEncode (content) {
const algorithmName = this.getSettingValue('algorithm')
return this.createDigest(algorithmName, content.getBytes())
}
/**
* Creates message digest using given algorithm.
* @protected
* @param {string} name Algorithm name
* @param {Uint8Array} message Message bytes
* @return {Promise}
*/
createDigest (name, message) {
const algorithm = algorithms.find(algorithm => algorithm.name === name)
switch (name) {
case 'md5':
return new Promise(resolve => resolve(md5(message)))
}
if (EnvUtil.isNode()) {
// Create message digest using Node Crypto async
return new Promise((resolve, reject) => {
const resultBuffer =
nodeCrypto.createHash(algorithm.nodeAlgorithm)
.update(global.Buffer.from(message))
.digest()
resolve(new Uint8Array(resultBuffer))
})
} else {
// Get crypto subtle instance
const crypto = window.crypto || window.msCrypto
const cryptoSubtle = crypto.subtle || crypto.webkitSubtle
// Create message digest using Web Crypto Api
let result = cryptoSubtle.digest(algorithm.browserAlgorithm, message)
// IE11 exception
if (result.oncomplete !== undefined) {
// Wrap IE11 CryptoOperation object in a promise
result = new Promise((resolve, reject) => {
result.oncomplete = resolve.bind(this, result.result)
result.onerror = reject
})
}
return result.then(buffer => new Uint8Array(buffer))
}
}
/**
* Returns algorithm objects available in the current environment.
* @protected
* @return {object[]}
*/
static filterAvailableAlgorithms () {
const isNode = EnvUtil.isNode()
return algorithms.filter(algorithm => {
// Algorithm availability not bound to the environment
if (algorithm.available === true) {
return true
}
// Browser environment
if (!isNode && algorithm.browserAlgorithm !== undefined) {
if (algorithm.browserExceptions !== undefined) {
return !EnvUtil.match.apply(EnvUtil, algorithm.browserExceptions)
}
return true
}
// Node environment
if (isNode && algorithm.nodeAlgorithm !== undefined) {
return true
}
return false
})
}
/**
* Returns block size (in bytes) for given algorithm name.
* @param {string} name Algorithm name
* @return {int|null} Block size integer or null, if algorithm is not defined
*/
static getAlgorithmBlockSize (name) {
const algorithm = algorithms.find(algorithm => algorithm.name === name)
return algorithm !== undefined ? algorithm.blockSize : null
}
}