on 685bb75 help sign in
bridge/src/lib/networkCode.js
55 lines 1.3 kB history raw
1import { hex2patp } from 'urbit-ob';
2import { createRing } from 'lib/keys';
4import jssha256 from 'js-sha256';
6function shax(buf) {
7 const hashed = jssha256.sha256.array(buf);
8 return Buffer.from(hashed);
9}
11function hex2buf(hex) {
12 return Buffer.from(hex, 'hex').reverse();
15function buf2hex(buf) {
16 return Buffer.from(buf)
17 .reverse()
18 .toString('hex');
21export function shas(buf, salt) {
22 return shax(xor(salt, shax(buf)));
25function xor(a, b) {
26 if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
27 console.log('a', a);
28 console.log('b', b);
29 throw new Error('only xor buffers!');
30 }
31 const length = Math.max(a.byteLength, b.byteLength);
32 const result = new Uint8Array(length);
33 for (let i = 0; i < length; i++) {
34 result[i] = a[i] ^ b[i];
35 }
36 return result;
39function shaf(buf, salt) {
40 const result = shas(buf, salt);
41 const halfway = result.length / 2;
42 const front = result.slice(0, halfway);
43 const back = result.slice(halfway, result.length);
44 return xor(front, back);
47export const generateCode = pair => {
48 const ring = hex2buf(createRing(pair));
49 const salt = hex2buf('73736170'); // salt is the noun %pass
50 const hash = shax(ring);
51 const result = shaf(hash, salt);
52 const half = result.slice(0, result.length / 2);
54 return hex2patp(buf2hex(half)).slice(1);
55};