1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 02:29:06 -05:00

Improve & refactor SHA-1 inside ws package (denoland/deno_std#137)

Original: 0a10ee9f93
This commit is contained in:
Max Graey 2019-01-24 17:55:24 +02:00 committed by Ryan Dahl
parent d8c99059ec
commit 306a5f701e
2 changed files with 118 additions and 115 deletions

View file

@ -9,77 +9,78 @@
/*jslint bitwise: true */ /*jslint bitwise: true */
const HEX_CHARS = "0123456789abcdef".split(""); const HEX_CHARS = "0123456789abcdef".split("");
const EXTRA = [-2147483648, 8388608, 32768, 128]; const EXTRA = Uint32Array.of(-2147483648, 8388608, 32768, 128);
const SHIFT = [24, 16, 8, 0]; const SHIFT = Uint32Array.of(24, 16, 8, 0);
const blocks = []; const blocks = new Uint32Array(80);
export class Sha1 { export class Sha1 {
blocks; private _blocks: Uint32Array;
block; private _block: number;
start; private _start: number;
bytes; private _bytes: number;
hBytes; private _hBytes: number;
finalized; private _finalized: boolean;
hashed; private _hashed: boolean;
first;
h0 = 0x67452301; private _h0 = 0x67452301;
h1 = 0xefcdab89; private _h1 = 0xefcdab89;
h2 = 0x98badcfe; private _h2 = 0x98badcfe;
h3 = 0x10325476; private _h3 = 0x10325476;
h4 = 0xc3d2e1f0; private _h4 = 0xc3d2e1f0;
lastByteIndex = 0; private _lastByteIndex = 0;
constructor(sharedMemory: boolean = false) { constructor(sharedMemory = false) {
if (sharedMemory) { if (sharedMemory) {
blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; this._blocks = blocks.fill(0, 0, 17);
this.blocks = blocks;
} else { } else {
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; this._blocks = new Uint32Array(80);
} }
this.h0 = 0x67452301; this._h0 = 0x67452301;
this.h1 = 0xefcdab89; this._h1 = 0xefcdab89;
this.h2 = 0x98badcfe; this._h2 = 0x98badcfe;
this.h3 = 0x10325476; this._h3 = 0x10325476;
this.h4 = 0xc3d2e1f0; this._h4 = 0xc3d2e1f0;
this.block = this.start = this.bytes = this.hBytes = 0; this._block = this._start = this._bytes = this._hBytes = 0;
this.finalized = this.hashed = false; this._finalized = this._hashed = false;
this.first = true;
} }
update(data: string | ArrayBuffer) { update(data: string | ArrayBuffer | ArrayBufferView) {
if (this.finalized) { if (this._finalized) {
return; return;
} }
let notString = true;
let message; let message;
let notString = typeof data !== "string"; if (data instanceof ArrayBuffer) {
if (notString && data instanceof ArrayBuffer) {
message = new Uint8Array(data); message = new Uint8Array(data);
} else if (ArrayBuffer.isView(data)) {
message = new Uint8Array(data.buffer);
} else { } else {
message = data; notString = false;
message = String(data);
} }
let code, let code,
index = 0, index = 0,
i, i,
start = this._start,
length = message.length || 0, length = message.length || 0,
blocks = this.blocks; blocks = this._blocks;
while (index < length) { while (index < length) {
if (this.hashed) { if (this._hashed) {
this.hashed = false; this._hashed = false;
blocks[0] = this.block; blocks[0] = this._block;
blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; blocks.fill(0, 1, 17);
} }
if (notString) { if (notString) {
for (i = this.start; index < length && i < 64; ++index) { for (i = start; index < length && i < 64; ++index) {
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
} }
} else { } else {
for (i = this.start; index < length && i < 64; ++index) { for (i = start; index < length && i < 64; ++index) {
code = message.charCodeAt(index); code = message.charCodeAt(index);
if (code < 0x80) { if (code < 0x80) {
blocks[i >> 2] |= code << SHIFT[i++ & 3]; blocks[i >> 2] |= code << SHIFT[i++ & 3];
@ -102,56 +103,55 @@ export class Sha1 {
} }
} }
this.lastByteIndex = i; this._lastByteIndex = i;
this.bytes += i - this.start; this._bytes += i - start;
if (i >= 64) { if (i >= 64) {
this.block = blocks[16]; this._block = blocks[16];
this.start = i - 64; this._start = i - 64;
this.hash(); this.hash();
this.hashed = true; this._hashed = true;
} else { } else {
this.start = i; this._start = i;
} }
} }
if (this.bytes > 4294967295) { if (this._bytes > 4294967295) {
this.hBytes += (this.bytes / 4294967296) << 0; this._hBytes += (this._bytes / 4294967296) >>> 0;
this.bytes = this.bytes % 4294967296; this._bytes = this._bytes >>> 0;
} }
return this;
} }
finalize() { finalize() {
if (this.finalized) { if (this._finalized) {
return; return;
} }
this.finalized = true; this._finalized = true;
let blocks = this.blocks, let blocks = this._blocks,
i = this.lastByteIndex; i = this._lastByteIndex;
blocks[16] = this.block; blocks[16] = this._block;
blocks[i >> 2] |= EXTRA[i & 3]; blocks[i >> 2] |= EXTRA[i & 3];
this.block = blocks[16]; this._block = blocks[16];
if (i >= 56) { if (i >= 56) {
if (!this.hashed) { if (!this._hashed) {
this.hash(); this.hash();
} }
blocks[0] = this.block; blocks[0] = this._block;
blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; blocks.fill(0, 1, 17);
} }
blocks[14] = (this.hBytes << 3) | (this.bytes >>> 29); blocks[14] = (this._hBytes << 3) | (this._bytes >>> 29);
blocks[15] = this.bytes << 3; blocks[15] = this._bytes << 3;
this.hash(); this.hash();
} }
hash() { hash() {
let a = this.h0, let a = this._h0,
b = this.h1, b = this._h1,
c = this.h2, c = this._h2,
d = this.h3, d = this._h3,
e = this.h4; e = this._h4;
let f, let f,
j, j,
t, t,
blocks = this.blocks; blocks = this._blocks;
for (j = 16; j < 80; ++j) { for (j = 16; j < 80; ++j) {
t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16]; t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
@ -161,126 +161,126 @@ export class Sha1 {
for (j = 0; j < 20; j += 5) { for (j = 0; j < 20; j += 5) {
f = (b & c) | (~b & d); f = (b & c) | (~b & d);
t = (a << 5) | (a >>> 27); t = (a << 5) | (a >>> 27);
e = (t + f + e + 1518500249 + blocks[j]) << 0; e = (t + f + e + 1518500249 + blocks[j]) >>> 0;
b = (b << 30) | (b >>> 2); b = (b << 30) | (b >>> 2);
f = (a & b) | (~a & c); f = (a & b) | (~a & c);
t = (e << 5) | (e >>> 27); t = (e << 5) | (e >>> 27);
d = (t + f + d + 1518500249 + blocks[j + 1]) << 0; d = (t + f + d + 1518500249 + blocks[j + 1]) >>> 0;
a = (a << 30) | (a >>> 2); a = (a << 30) | (a >>> 2);
f = (e & a) | (~e & b); f = (e & a) | (~e & b);
t = (d << 5) | (d >>> 27); t = (d << 5) | (d >>> 27);
c = (t + f + c + 1518500249 + blocks[j + 2]) << 0; c = (t + f + c + 1518500249 + blocks[j + 2]) >>> 0;
e = (e << 30) | (e >>> 2); e = (e << 30) | (e >>> 2);
f = (d & e) | (~d & a); f = (d & e) | (~d & a);
t = (c << 5) | (c >>> 27); t = (c << 5) | (c >>> 27);
b = (t + f + b + 1518500249 + blocks[j + 3]) << 0; b = (t + f + b + 1518500249 + blocks[j + 3]) >>> 0;
d = (d << 30) | (d >>> 2); d = (d << 30) | (d >>> 2);
f = (c & d) | (~c & e); f = (c & d) | (~c & e);
t = (b << 5) | (b >>> 27); t = (b << 5) | (b >>> 27);
a = (t + f + a + 1518500249 + blocks[j + 4]) << 0; a = (t + f + a + 1518500249 + blocks[j + 4]) >>> 0;
c = (c << 30) | (c >>> 2); c = (c << 30) | (c >>> 2);
} }
for (; j < 40; j += 5) { for (; j < 40; j += 5) {
f = b ^ c ^ d; f = b ^ c ^ d;
t = (a << 5) | (a >>> 27); t = (a << 5) | (a >>> 27);
e = (t + f + e + 1859775393 + blocks[j]) << 0; e = (t + f + e + 1859775393 + blocks[j]) >>> 0;
b = (b << 30) | (b >>> 2); b = (b << 30) | (b >>> 2);
f = a ^ b ^ c; f = a ^ b ^ c;
t = (e << 5) | (e >>> 27); t = (e << 5) | (e >>> 27);
d = (t + f + d + 1859775393 + blocks[j + 1]) << 0; d = (t + f + d + 1859775393 + blocks[j + 1]) >>> 0;
a = (a << 30) | (a >>> 2); a = (a << 30) | (a >>> 2);
f = e ^ a ^ b; f = e ^ a ^ b;
t = (d << 5) | (d >>> 27); t = (d << 5) | (d >>> 27);
c = (t + f + c + 1859775393 + blocks[j + 2]) << 0; c = (t + f + c + 1859775393 + blocks[j + 2]) >>> 0;
e = (e << 30) | (e >>> 2); e = (e << 30) | (e >>> 2);
f = d ^ e ^ a; f = d ^ e ^ a;
t = (c << 5) | (c >>> 27); t = (c << 5) | (c >>> 27);
b = (t + f + b + 1859775393 + blocks[j + 3]) << 0; b = (t + f + b + 1859775393 + blocks[j + 3]) >>> 0;
d = (d << 30) | (d >>> 2); d = (d << 30) | (d >>> 2);
f = c ^ d ^ e; f = c ^ d ^ e;
t = (b << 5) | (b >>> 27); t = (b << 5) | (b >>> 27);
a = (t + f + a + 1859775393 + blocks[j + 4]) << 0; a = (t + f + a + 1859775393 + blocks[j + 4]) >>> 0;
c = (c << 30) | (c >>> 2); c = (c << 30) | (c >>> 2);
} }
for (; j < 60; j += 5) { for (; j < 60; j += 5) {
f = (b & c) | (b & d) | (c & d); f = (b & c) | (b & d) | (c & d);
t = (a << 5) | (a >>> 27); t = (a << 5) | (a >>> 27);
e = (t + f + e - 1894007588 + blocks[j]) << 0; e = (t + f + e - 1894007588 + blocks[j]) >>> 0;
b = (b << 30) | (b >>> 2); b = (b << 30) | (b >>> 2);
f = (a & b) | (a & c) | (b & c); f = (a & b) | (a & c) | (b & c);
t = (e << 5) | (e >>> 27); t = (e << 5) | (e >>> 27);
d = (t + f + d - 1894007588 + blocks[j + 1]) << 0; d = (t + f + d - 1894007588 + blocks[j + 1]) >>> 0;
a = (a << 30) | (a >>> 2); a = (a << 30) | (a >>> 2);
f = (e & a) | (e & b) | (a & b); f = (e & a) | (e & b) | (a & b);
t = (d << 5) | (d >>> 27); t = (d << 5) | (d >>> 27);
c = (t + f + c - 1894007588 + blocks[j + 2]) << 0; c = (t + f + c - 1894007588 + blocks[j + 2]) >>> 0;
e = (e << 30) | (e >>> 2); e = (e << 30) | (e >>> 2);
f = (d & e) | (d & a) | (e & a); f = (d & e) | (d & a) | (e & a);
t = (c << 5) | (c >>> 27); t = (c << 5) | (c >>> 27);
b = (t + f + b - 1894007588 + blocks[j + 3]) << 0; b = (t + f + b - 1894007588 + blocks[j + 3]) >>> 0;
d = (d << 30) | (d >>> 2); d = (d << 30) | (d >>> 2);
f = (c & d) | (c & e) | (d & e); f = (c & d) | (c & e) | (d & e);
t = (b << 5) | (b >>> 27); t = (b << 5) | (b >>> 27);
a = (t + f + a - 1894007588 + blocks[j + 4]) << 0; a = (t + f + a - 1894007588 + blocks[j + 4]) >>> 0;
c = (c << 30) | (c >>> 2); c = (c << 30) | (c >>> 2);
} }
for (; j < 80; j += 5) { for (; j < 80; j += 5) {
f = b ^ c ^ d; f = b ^ c ^ d;
t = (a << 5) | (a >>> 27); t = (a << 5) | (a >>> 27);
e = (t + f + e - 899497514 + blocks[j]) << 0; e = (t + f + e - 899497514 + blocks[j]) >>> 0;
b = (b << 30) | (b >>> 2); b = (b << 30) | (b >>> 2);
f = a ^ b ^ c; f = a ^ b ^ c;
t = (e << 5) | (e >>> 27); t = (e << 5) | (e >>> 27);
d = (t + f + d - 899497514 + blocks[j + 1]) << 0; d = (t + f + d - 899497514 + blocks[j + 1]) >>> 0;
a = (a << 30) | (a >>> 2); a = (a << 30) | (a >>> 2);
f = e ^ a ^ b; f = e ^ a ^ b;
t = (d << 5) | (d >>> 27); t = (d << 5) | (d >>> 27);
c = (t + f + c - 899497514 + blocks[j + 2]) << 0; c = (t + f + c - 899497514 + blocks[j + 2]) >>> 0;
e = (e << 30) | (e >>> 2); e = (e << 30) | (e >>> 2);
f = d ^ e ^ a; f = d ^ e ^ a;
t = (c << 5) | (c >>> 27); t = (c << 5) | (c >>> 27);
b = (t + f + b - 899497514 + blocks[j + 3]) << 0; b = (t + f + b - 899497514 + blocks[j + 3]) >>> 0;
d = (d << 30) | (d >>> 2); d = (d << 30) | (d >>> 2);
f = c ^ d ^ e; f = c ^ d ^ e;
t = (b << 5) | (b >>> 27); t = (b << 5) | (b >>> 27);
a = (t + f + a - 899497514 + blocks[j + 4]) << 0; a = (t + f + a - 899497514 + blocks[j + 4]) >>> 0;
c = (c << 30) | (c >>> 2); c = (c << 30) | (c >>> 2);
} }
this.h0 = (this.h0 + a) << 0; this._h0 = (this._h0 + a) >>> 0;
this.h1 = (this.h1 + b) << 0; this._h1 = (this._h1 + b) >>> 0;
this.h2 = (this.h2 + c) << 0; this._h2 = (this._h2 + c) >>> 0;
this.h3 = (this.h3 + d) << 0; this._h3 = (this._h3 + d) >>> 0;
this.h4 = (this.h4 + e) << 0; this._h4 = (this._h4 + e) >>> 0;
} }
hex() { hex() {
this.finalize(); this.finalize();
let h0 = this.h0, let h0 = this._h0,
h1 = this.h1, h1 = this._h1,
h2 = this.h2, h2 = this._h2,
h3 = this.h3, h3 = this._h3,
h4 = this.h4; h4 = this._h4;
return ( return (
HEX_CHARS[(h0 >> 28) & 0x0f] + HEX_CHARS[(h0 >> 28) & 0x0f] +
@ -333,11 +333,11 @@ export class Sha1 {
digest() { digest() {
this.finalize(); this.finalize();
let h0 = this.h0, let h0 = this._h0,
h1 = this.h1, h1 = this._h1,
h2 = this.h2, h2 = this._h2,
h3 = this.h3, h3 = this._h3,
h4 = this.h4; h4 = this._h4;
return [ return [
(h0 >> 24) & 0xff, (h0 >> 24) & 0xff,
@ -369,14 +369,8 @@ export class Sha1 {
arrayBuffer() { arrayBuffer() {
this.finalize(); this.finalize();
return Uint32Array.of(
let buffer = new ArrayBuffer(20); this._h0, this._h1, this._h2, this._h3, this._h4
let dataView = new DataView(buffer); ).buffer;
dataView.setUint32(0, this.h0);
dataView.setUint32(4, this.h1);
dataView.setUint32(8, this.h2);
dataView.setUint32(12, this.h3);
dataView.setUint32(16, this.h4);
return buffer;
} }
} }

View file

@ -1,8 +1,17 @@
import { assertEqual, test } from "../testing/mod.ts"; import { assert, test } from "../testing/mod.ts";
import { Sha1 } from "./sha1.ts"; import { Sha1 } from "./sha1.ts";
test(function testSha1() { test(function testSha1() {
const sha1 = new Sha1(); let sha1 = new Sha1();
sha1.update("abcde"); sha1.update("abcde");
assertEqual(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); assert.equal(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334");
const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65);
sha1 = new Sha1();
sha1.update(data);
assert.equal(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334");
sha1 = new Sha1();
sha1.update(data.buffer);
assert.equal(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334");
}); });