-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
45 lines (38 loc) · 1 KB
/
helpers.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
/*
* Helpers for various tasks
*
*/
// Dependencies
const crypto = require('crypto')
const config = require('./config')
const {
hashingSecret
} = require('./config')
const possibleCharacters = 'abcdefghijklmnopqrstuvxyz0123456789'
module.exports = {
/**
* Create a sha256 hash
*/
hash: (password) => {
if (typeof password !== 'string' || !password.length) {
return false
}
// TODO: Consider replacing with crypto.pbkdf2(...) after done with training
return crypto
.createHmac('sha256', hashingSecret)
.update(password)
.digest('hex')
},
createRandomString: (length) => {
if(typeof length !== 'number' || length <= 0 || length !== Math.floor(length)) {
return false
}
let result = ''
for (let i = 0; i < length; i++) {
const randomPosition = Math.floor(Math.random() * possibleCharacters.length)
const selectedCharacther = possibleCharacters.charAt(randomPosition)
result += selectedCharacther
}
return result
}
}