on b687c79 help sign in
bridge/src/lib/networkCode.js
49 lines 1.1 kB history raw
1import { hex2patp } from 'urbit-ob';
3import jssha256 from 'js-sha256';
5function shax(buf) {
6 const hashed = jssha256.sha256.array(buf);
7 return Buffer.from(hashed);
8}
10function hexToArrayBuffer(hex) {
11 return Buffer.from(hex, 'hex').reverse();
14function buf2hex(buf) {
15 return Buffer.from(buf)
16 .reverse()
17 .toString('hex');
20function shas(buf, salt) {
21 return shax(xor(salt, shax(buf)));
24function xor(a, b) {
25 const length = Math.max(a.byteLength, b.byteLength);
26 const result = new Uint8Array(length);
27 for (let i = 0; i < length; i++) {
28 result[i] = a[i] ^ b[i];
29 }
30 return result;
33function shaf(buf, salt) {
34 const result = shas(buf, salt);
35 const halfway = result.length / 2;
36 const front = result.slice(0, halfway);
37 const back = result.slice(halfway, result.length);
38 return xor(front, back);
41export const generateCode = async privKeyHex => {
42 const privKey = hexToArrayBuffer(privKeyHex);
43 const salt = hexToArrayBuffer('73736170');
44 const hash = shax(privKey);
45 const result = shaf(hash, salt);
46 const half = result.slice(0, result.length / 2);
48 return hex2patp(buf2hex(half));
49};