-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedcryption.js
More file actions
61 lines (50 loc) · 1.77 KB
/
Copy pathedcryption.js
File metadata and controls
61 lines (50 loc) · 1.77 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
const bcrypt = require('bcryptjs');
const crypto = require('crypto')
const md5 = crypto.createHash('md5')
class EDCryption {
constructor() {
}
md5HashSync(rawString) {
return md5.update(rawString, 'utf8').digest('hex')
}
bcryptHashSync(rawString, saltRounds = 10) {
return bcrypt.hashSync(rawString, saltRounds)
}
bcryptCompareSync(rawString, hashString) {
return bcrypt.compareSync(rawString, hashString)
}
async bcryptHashAsync(rawString, saltRounds = 10, callback) {
if(callback && typeof callback === "function") {
bcrypt.hash(rawString, saltRounds, (error, data) => {
callback(error, data);
})
} else {
return new Promise(async (resolve, reject) => {
try {
const data = await bcrypt.hash(rawString, saltRounds)
return resolve(data)
} catch (error) {
return reject(error)
}
})
}
}
async bcryptCompareAsync(rawString, hashString, callback) {
if(callback && typeof callback === "function") {
bcrypt.compare(rawString, hashString, (error, result) => {
callback(error, result)
})
} else {
return new Promise(async (resolve, reject) => {
try {
const result = await bcrypt.compare(rawString, hashString)
return resolve(result)
} catch (error) {
return reject(error)
}
})
}
}
}
const EDCryptionShareInstance = new EDCryption()
module.exports = EDCryptionShareInstance