Nodejs

No description

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
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
const readlineSync = require('readline-sync');

// We'll represent a 32-bit word
// as an array of 4 bytes

const _ = undefined;

function add(v, w) {
  const s = [_, _, _, _];
  const c = [0, 0, 0, 0];

  for (let i = v.length-1; i >= 0; i--) {
    s[i] = v[i] + w[i] + c[i];

    while (s[i] > 0xff) {
      s[i] -= 256;
      if (i > 0) {
        c[i-1]++;
      }
    }
  }

  return s;
}

function toBytes(n) {
  const b = [];
  for (let i = 8; i > 0; i--) {
    b.push(n / 256**i);
    n = n % 256**i;
  }

  return b;
}

function sha1(_m) { // m for message
  const m = _m.slice();

  // STEPS 1-4: Preprocessing:

  // STEP 1: Initialize 5 32-bit words.

  const h = [[0x67, 0x45, 0x23, 0x01],
             [0xEF, 0xCD, 0xAB, 0x89],
             [0x98, 0xBA, 0xDC, 0xFE],
             [0x10, 0x32, 0x54, 0x76],
             [0xC3, 0xD2, 0xE1, 0xF0]];

  // STEP 2 [optional]: Pad the message.
  // If the length is not a multiple of 512 bits,
  // this step is required.

  const l = m.length % 64;
  if (l !== 0) {
    if (l <= 56 && l !== 0) {
      m.push(0x80);
      m.push(...Array(48 - l).fill(0));
    }
    m.push(...toBytes(l));
  }

  // STEP 3: Divide the message into
  // 512-bit chunks.

  const C = []; // c for chunks
  for (let i = 0; i < m.length; i += 64) {
    C.push(m.slice(i, i+64));
  }

  // STEP 4: For each chunk...
  for (let c of C) {
    const w = c.slice();

    for (let i = 16; i < 79; i++) {
      w[i] 
    }
  }
}

while (true) {
  let v = readlineSync.question('Word:').split(/\W/).map(Number);
  let w = readlineSync.question('Word:').split(/\W/).map(Number);
  console.log('Sum: ' + add(v, w));
}
node v10.16.0