-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (116 loc) · 3.57 KB
/
index.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
var SHA256 = require("crypto-js/sha256");
var fs = require("fs");
var path = require("path");
var NOUN = fs.readFileSync(path.join(__dirname, 'data', 'noun.dat')).toString().split('\n');
var ADJ = fs.readFileSync(path.join(__dirname, 'data', 'adj.dat')).toString().split('\n');
var ADV = fs.readFileSync(path.join(__dirname, 'data', 'adv.dat')).toString().split('\n');
// Adds two arrays for the given base (10 or 16), returning the result.
// This turns out to be the only "primitive" operation we need.
function add(x, y, base) {
var z = [];
var n = Math.max(x.length, y.length);
var carry = 0;
var i = 0;
while (i < n || carry) {
var xi = i < x.length ? x[i] : 0;
var yi = i < y.length ? y[i] : 0;
var zi = carry + xi + yi;
z.push(zi % base);
carry = Math.floor(zi / base);
i++;
}
return z;
}
// Returns a*x, where x is an array of decimal digits and a is an ordinary
// JavaScript number. base is the number base of the array x.
function multiplyByNumber(num, x, base) {
if (num < 0) return null;
if (num == 0) return [];
var result = [];
var power = x;
while (true) {
if (num & 1) {
result = add(result, power, base);
}
num = num >> 1;
if (num === 0) break;
power = add(power, power, base);
}
return result;
}
function parseToDigitsArray(str, base) {
var digits = str.split('');
var ary = [];
for (var i = digits.length - 1; i >= 0; i--) {
var n = parseInt(digits[i], base);
if (isNaN(n)) return null;
ary.push(n);
}
return ary;
}
function convertBase(str, fromBase, toBase) {
var digits = parseToDigitsArray(str, fromBase);
if (digits === null) return null;
var outArray = [];
var power = [1];
for (var i = 0; i < digits.length; i++) {
// invariant: at this point, fromBase^i = power
if (digits[i]) {
outArray = add(outArray, multiplyByNumber(digits[i], power, toBase), toBase);
}
power = multiplyByNumber(fromBase, power, toBase);
}
var out = '';
for (var i = outArray.length - 1; i >= 0; i--) {
out += outArray[i].toString(toBase);
}
return out;
}
function decToHex(decStr) {
var hex = convertBase(decStr, 10, 16);
return hex ? '0x' + hex : null;
}
function hexToDec(hexStr) {
if (hexStr.substring(0, 2) === '0x') hexStr = hexStr.substring(2);
hexStr = hexStr.toLowerCase();
return convertBase(hexStr, 16, 10);
}
module.exports = function(length, delimiter, hasher, noun, adj, adv) {
length = typeof length !== 'undefined' ? length : 3;
delimiter = typeof delimiter !== 'undefined' ? delimiter : "-";
hasher = typeof hasher !== 'undefined' ? hasher : SHA256;
noun = typeof noun !== 'undefined' ? noun : NOUN;
adj = typeof adj !== 'undefined' ? adj : ADJ;
adv = typeof adv !== 'undefined' ? adv : ADV;
var str2indexes = function(hex) {
var sha1 = hasher(hex).toString();
console.log(sha1);
var seg = sha1.length / length;
var rem = sha1.length % length;
var retVal = [];
for (var i = 0; i < length; i++) {
retVal.push(parseInt(hexToDec(sha1.substring(seg*i, seg*(i+1)))));
}
return retVal;
};
hash = function(str) {
var indexes = str2indexes(str);
var noun_idx = indexes.pop();
var output = [];
for (var i = 0; i < indexes.length; i++) {
if (i % 2 == 0)
output.push(adv[indexes[i] % adv.length]);
else
output.push(adj[indexes[i] % adj.length]);
}
output.push(noun[noun_idx % noun.length]);
return output.join(delimiter);
};
return {
hash: hash,
rand: function() {
var buf = crypto.randomBytes(256);
return hash(buf.toString());
}
};
}