-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhashToParts.js
More file actions
71 lines (65 loc) · 1.91 KB
/
hashToParts.js
File metadata and controls
71 lines (65 loc) · 1.91 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
var SHA256 = require("crypto-js/sha256");
const parts = {
bd: {
body: 'normal-body',
eyes: 'bd-eyes',
hair: 'bd-hair',
head: 'normal-head',
mounth: 'normal-mounth',
},
hg: {
body: 'coding-body',
eyes: 'hg-eyes',
hair: 'hg-hair',
head: 'normal-head',
mounth: 'normal-mounth',
},
pg: {
body: 'normal-body',
eyes: 'pg-eyes',
hair: 'pg-hair',
head: 'pg-head',
mounth: 'pg-mounth',
},
rg: {
body: 'rg-body',
eyes: 'rg-eyes',
hair: 'rg-hair',
head: 'normal-head',
mounth: 'rg-mounth',
},
sg: {
body: 'sg-body',
eyes: 'sg-eyes',
hair: 'sg-hair',
head: 'sg-head',
mounth: 'sg-mounth',
},
}
const hashPartMapper = {
00: 'bd',
01: 'hg',
02: 'pg',
03: 'rg',
04: 'sg',
}
const hashToParts = (text) => {
const sha256Hash = SHA256(text).toString();
const sha256Numbers = sha256Hash.toString().replace(/\D/g,'');
const numbersParts = sha256Numbers.slice(0, 10)
const numbersPartsMapper = {
body: hashPartMapper[Math.round((50/100) * `${numbersParts[0]}${numbersParts[1]}`) % 4],
eyes: hashPartMapper[Math.round((50/100) * `${numbersParts[2]}${numbersParts[3]}`) % 4],
hair: hashPartMapper[Math.round((50/100) * `${numbersParts[4]}${numbersParts[5]}`) % 4],
head: hashPartMapper[Math.round((50/100) * `${numbersParts[6]}${numbersParts[7]}`) % 4],
mounth: hashPartMapper[Math.round((50/100) * `${numbersParts[8]}${numbersParts[9]}`) % 4],
}
return {
body: parts[numbersPartsMapper.body].body,
eyes: parts[numbersPartsMapper.eyes].eyes,
hair: parts[numbersPartsMapper.hair].hair,
head: parts[numbersPartsMapper.head].head,
mounth: parts[numbersPartsMapper.mounth].mounth,
}
}
module.exports = hashToParts