From df0000ff0a3ce20292fe73b909cd31bd352d5266 Mon Sep 17 00:00:00 2001 From: underfin <2218301630@qq.com> Date: Mon, 27 Apr 2020 20:49:34 +0800 Subject: feat(std/uuid): Implement uuid v5 (#4916) --- std/util/sha1.ts | 375 ++++++++++++++++++++++++++++++++++++++++++ std/util/sha1_test.ts | 24 +++ std/uuid/_common.ts | 31 +++- std/uuid/mod.ts | 6 +- std/uuid/test.ts | 4 + std/uuid/tests/v5/generate.ts | 66 ++++++++ std/uuid/tests/v5/validate.ts | 19 +++ std/uuid/v5.ts | 52 ++++++ std/ws/mod.ts | 2 +- std/ws/sha1.ts | 374 ----------------------------------------- std/ws/sha1_test.ts | 24 --- 11 files changed, 573 insertions(+), 404 deletions(-) create mode 100644 std/util/sha1.ts create mode 100644 std/util/sha1_test.ts create mode 100644 std/uuid/tests/v5/generate.ts create mode 100644 std/uuid/tests/v5/validate.ts create mode 100644 std/uuid/v5.ts delete mode 100644 std/ws/sha1.ts delete mode 100644 std/ws/sha1_test.ts (limited to 'std') diff --git a/std/util/sha1.ts b/std/util/sha1.ts new file mode 100644 index 000000000..bb9103d27 --- /dev/null +++ b/std/util/sha1.ts @@ -0,0 +1,375 @@ +/* + * [js-sha1]{@link https://github.com/emn178/js-sha1} + * + * @version 0.6.0 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2017 + * @license MIT + */ +/*jslint bitwise: true */ + +const HEX_CHARS = "0123456789abcdef".split(""); +const EXTRA = Uint32Array.of(-2147483648, 8388608, 32768, 128); +const SHIFT = Uint32Array.of(24, 16, 8, 0); + +const blocks = new Uint32Array(80); + +export class Sha1 { + private _blocks: Uint32Array; + private _block: number; + private _start: number; + private _bytes: number; + private _hBytes: number; + private _finalized: boolean; + private _hashed: boolean; + + private _h0 = 0x67452301; + private _h1 = 0xefcdab89; + private _h2 = 0x98badcfe; + private _h3 = 0x10325476; + private _h4 = 0xc3d2e1f0; + private _lastByteIndex = 0; + + constructor(sharedMemory = false) { + if (sharedMemory) { + this._blocks = blocks.fill(0, 0, 17); + } else { + this._blocks = new Uint32Array(80); + } + + this._h0 = 0x67452301; + this._h1 = 0xefcdab89; + this._h2 = 0x98badcfe; + this._h3 = 0x10325476; + this._h4 = 0xc3d2e1f0; + + this._block = this._start = this._bytes = this._hBytes = 0; + this._finalized = this._hashed = false; + } + + update(data: string | ArrayBuffer | ArrayBufferView): Sha1 { + if (this._finalized) { + return this; + } + let notString = true; + let message; + if (data instanceof ArrayBuffer) { + message = new Uint8Array(data); + } else if (ArrayBuffer.isView(data)) { + message = new Uint8Array(data.buffer); + } else { + notString = false; + message = String(data); + } + let code; + let index = 0; + let i; + const start = this._start; + const length = message.length || 0; + const blocks = this._blocks; + + while (index < length) { + if (this._hashed) { + this._hashed = false; + blocks[0] = this._block; + blocks.fill(0, 1, 17); + } + + if (notString) { + for (i = start; index < length && i < 64; ++index) { + blocks[i >> 2] |= (message[index] as number) << SHIFT[i++ & 3]; + } + } else { + for (i = start; index < length && i < 64; ++index) { + code = (message as string).charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = + 0x10000 + + (((code & 0x3ff) << 10) | + ((message as string).charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + } + + this._lastByteIndex = i; + this._bytes += i - start; + if (i >= 64) { + this._block = blocks[16]; + this._start = i - 64; + this.hash(); + this._hashed = true; + } else { + this._start = i; + } + } + if (this._bytes > 4294967295) { + this._hBytes += (this._bytes / 4294967296) >>> 0; + this._bytes = this._bytes >>> 0; + } + return this; + } + + finalize(): void { + if (this._finalized) { + return; + } + this._finalized = true; + const blocks = this._blocks; + const i = this._lastByteIndex; + blocks[16] = this._block; + blocks[i >> 2] |= EXTRA[i & 3]; + this._block = blocks[16]; + if (i >= 56) { + if (!this._hashed) { + this.hash(); + } + blocks[0] = this._block; + blocks.fill(0, 1, 17); + } + blocks[14] = (this._hBytes << 3) | (this._bytes >>> 29); + blocks[15] = this._bytes << 3; + this.hash(); + } + + hash(): void { + let a = this._h0; + let b = this._h1; + let c = this._h2; + let d = this._h3; + let e = this._h4; + let f, j, t; + const blocks = this._blocks; + + for (j = 16; j < 80; ++j) { + t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16]; + blocks[j] = (t << 1) | (t >>> 31); + } + + for (j = 0; j < 20; j += 5) { + f = (b & c) | (~b & d); + t = (a << 5) | (a >>> 27); + e = (t + f + e + 1518500249 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = (a & b) | (~a & c); + t = (e << 5) | (e >>> 27); + d = (t + f + d + 1518500249 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = (e & a) | (~e & b); + t = (d << 5) | (d >>> 27); + c = (t + f + c + 1518500249 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = (d & e) | (~d & a); + t = (c << 5) | (c >>> 27); + b = (t + f + b + 1518500249 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = (c & d) | (~c & e); + t = (b << 5) | (b >>> 27); + a = (t + f + a + 1518500249 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + for (; j < 40; j += 5) { + f = b ^ c ^ d; + t = (a << 5) | (a >>> 27); + e = (t + f + e + 1859775393 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = a ^ b ^ c; + t = (e << 5) | (e >>> 27); + d = (t + f + d + 1859775393 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = e ^ a ^ b; + t = (d << 5) | (d >>> 27); + c = (t + f + c + 1859775393 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = d ^ e ^ a; + t = (c << 5) | (c >>> 27); + b = (t + f + b + 1859775393 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = c ^ d ^ e; + t = (b << 5) | (b >>> 27); + a = (t + f + a + 1859775393 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + for (; j < 60; j += 5) { + f = (b & c) | (b & d) | (c & d); + t = (a << 5) | (a >>> 27); + e = (t + f + e - 1894007588 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = (a & b) | (a & c) | (b & c); + t = (e << 5) | (e >>> 27); + d = (t + f + d - 1894007588 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = (e & a) | (e & b) | (a & b); + t = (d << 5) | (d >>> 27); + c = (t + f + c - 1894007588 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = (d & e) | (d & a) | (e & a); + t = (c << 5) | (c >>> 27); + b = (t + f + b - 1894007588 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = (c & d) | (c & e) | (d & e); + t = (b << 5) | (b >>> 27); + a = (t + f + a - 1894007588 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + for (; j < 80; j += 5) { + f = b ^ c ^ d; + t = (a << 5) | (a >>> 27); + e = (t + f + e - 899497514 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = a ^ b ^ c; + t = (e << 5) | (e >>> 27); + d = (t + f + d - 899497514 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = e ^ a ^ b; + t = (d << 5) | (d >>> 27); + c = (t + f + c - 899497514 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = d ^ e ^ a; + t = (c << 5) | (c >>> 27); + b = (t + f + b - 899497514 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = c ^ d ^ e; + t = (b << 5) | (b >>> 27); + a = (t + f + a - 899497514 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + this._h0 = (this._h0 + a) >>> 0; + this._h1 = (this._h1 + b) >>> 0; + this._h2 = (this._h2 + c) >>> 0; + this._h3 = (this._h3 + d) >>> 0; + this._h4 = (this._h4 + e) >>> 0; + } + + hex(): string { + this.finalize(); + + const h0 = this._h0; + const h1 = this._h1; + const h2 = this._h2; + const h3 = this._h3; + const h4 = this._h4; + + return ( + HEX_CHARS[(h0 >> 28) & 0x0f] + + HEX_CHARS[(h0 >> 24) & 0x0f] + + HEX_CHARS[(h0 >> 20) & 0x0f] + + HEX_CHARS[(h0 >> 16) & 0x0f] + + HEX_CHARS[(h0 >> 12) & 0x0f] + + HEX_CHARS[(h0 >> 8) & 0x0f] + + HEX_CHARS[(h0 >> 4) & 0x0f] + + HEX_CHARS[h0 & 0x0f] + + HEX_CHARS[(h1 >> 28) & 0x0f] + + HEX_CHARS[(h1 >> 24) & 0x0f] + + HEX_CHARS[(h1 >> 20) & 0x0f] + + HEX_CHARS[(h1 >> 16) & 0x0f] + + HEX_CHARS[(h1 >> 12) & 0x0f] + + HEX_CHARS[(h1 >> 8) & 0x0f] + + HEX_CHARS[(h1 >> 4) & 0x0f] + + HEX_CHARS[h1 & 0x0f] + + HEX_CHARS[(h2 >> 28) & 0x0f] + + HEX_CHARS[(h2 >> 24) & 0x0f] + + HEX_CHARS[(h2 >> 20) & 0x0f] + + HEX_CHARS[(h2 >> 16) & 0x0f] + + HEX_CHARS[(h2 >> 12) & 0x0f] + + HEX_CHARS[(h2 >> 8) & 0x0f] + + HEX_CHARS[(h2 >> 4) & 0x0f] + + HEX_CHARS[h2 & 0x0f] + + HEX_CHARS[(h3 >> 28) & 0x0f] + + HEX_CHARS[(h3 >> 24) & 0x0f] + + HEX_CHARS[(h3 >> 20) & 0x0f] + + HEX_CHARS[(h3 >> 16) & 0x0f] + + HEX_CHARS[(h3 >> 12) & 0x0f] + + HEX_CHARS[(h3 >> 8) & 0x0f] + + HEX_CHARS[(h3 >> 4) & 0x0f] + + HEX_CHARS[h3 & 0x0f] + + HEX_CHARS[(h4 >> 28) & 0x0f] + + HEX_CHARS[(h4 >> 24) & 0x0f] + + HEX_CHARS[(h4 >> 20) & 0x0f] + + HEX_CHARS[(h4 >> 16) & 0x0f] + + HEX_CHARS[(h4 >> 12) & 0x0f] + + HEX_CHARS[(h4 >> 8) & 0x0f] + + HEX_CHARS[(h4 >> 4) & 0x0f] + + HEX_CHARS[h4 & 0x0f] + ); + } + + toString(): string { + return this.hex(); + } + + digest(): number[] { + this.finalize(); + + const h0 = this._h0; + const h1 = this._h1; + const h2 = this._h2; + const h3 = this._h3; + const h4 = this._h4; + + return [ + (h0 >> 24) & 0xff, + (h0 >> 16) & 0xff, + (h0 >> 8) & 0xff, + h0 & 0xff, + (h1 >> 24) & 0xff, + (h1 >> 16) & 0xff, + (h1 >> 8) & 0xff, + h1 & 0xff, + (h2 >> 24) & 0xff, + (h2 >> 16) & 0xff, + (h2 >> 8) & 0xff, + h2 & 0xff, + (h3 >> 24) & 0xff, + (h3 >> 16) & 0xff, + (h3 >> 8) & 0xff, + h3 & 0xff, + (h4 >> 24) & 0xff, + (h4 >> 16) & 0xff, + (h4 >> 8) & 0xff, + h4 & 0xff, + ]; + } + + array(): number[] { + return this.digest(); + } + + arrayBuffer(): ArrayBuffer { + this.finalize(); + return Uint32Array.of(this._h0, this._h1, this._h2, this._h3, this._h4) + .buffer; + } +} diff --git a/std/util/sha1_test.ts b/std/util/sha1_test.ts new file mode 100644 index 000000000..159bdd94d --- /dev/null +++ b/std/util/sha1_test.ts @@ -0,0 +1,24 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +const { test } = Deno; +import { assertEquals } from "../testing/asserts.ts"; +import { Sha1 } from "./sha1.ts"; + +test("[util/sha] test1", () => { + const sha1 = new Sha1(); + sha1.update("abcde"); + assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); +}); + +test("[util/sha] testWithArray", () => { + const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); + const sha1 = new Sha1(); + sha1.update(data); + assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); +}); + +test("[util/sha] testSha1WithBuffer", () => { + const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); + const sha1 = new Sha1(); + sha1.update(data.buffer); + assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); +}); diff --git a/std/uuid/_common.ts b/std/uuid/_common.ts index b0cad2584..8f13ac7e3 100644 --- a/std/uuid/_common.ts +++ b/std/uuid/_common.ts @@ -13,6 +13,35 @@ export function bytesToUuid(bytes: number[] | Uint8Array): string { "-", ...bits.slice(8, 10), "-", - ...bits.slice(10), + ...bits.slice(10, 16), ].join(""); } + +export function uuidToBytes(uuid: string): number[] { + const bytes: number[] = []; + + uuid.replace(/[a-fA-F0-9]{2}/g, (hex: string): string => { + bytes.push(parseInt(hex, 16)); + return ""; + }); + + return bytes; +} + +export function stringToBytes(str: string): number[] { + str = unescape(encodeURIComponent(str)); + const bytes = new Array(str.length); + for (let i = 0; i < str.length; i++) { + bytes[i] = str.charCodeAt(i); + } + return bytes; +} + +export function createBuffer(content: number[]): ArrayBuffer { + const arrayBuffer = new ArrayBuffer(content.length); + const uint8Array = new Uint8Array(arrayBuffer); + for (let i = 0; i < content.length; i++) { + uint8Array[i] = content[i]; + } + return arrayBuffer; +} diff --git a/std/uuid/mod.ts b/std/uuid/mod.ts index 23d0c6410..7ffbb1349 100644 --- a/std/uuid/mod.ts +++ b/std/uuid/mod.ts @@ -2,6 +2,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as v1 from "./v1.ts"; import * as v4 from "./v4.ts"; +import * as v5 from "./v5.ts"; export const NIL_UUID = "00000000-0000-0000-0000-000000000000"; @@ -21,7 +22,4 @@ const NOT_IMPLEMENTED = { // TODO Implement export const v3 = NOT_IMPLEMENTED; -export { v1, v4 }; - -// TODO Implement -export const v5 = NOT_IMPLEMENTED; +export { v1, v4, v5 }; diff --git a/std/uuid/test.ts b/std/uuid/test.ts index 74d3c093d..58eae2fff 100755 --- a/std/uuid/test.ts +++ b/std/uuid/test.ts @@ -11,3 +11,7 @@ import "./tests/v1/generate.ts"; // V4 Tests import "./tests/v4/validate.ts"; import "./tests/v4/generate.ts"; + +// V5 Tests +import "./tests/v5/validate.ts"; +import "./tests/v5/generate.ts"; diff --git a/std/uuid/tests/v5/generate.ts b/std/uuid/tests/v5/generate.ts new file mode 100644 index 000000000..c869ef505 --- /dev/null +++ b/std/uuid/tests/v5/generate.ts @@ -0,0 +1,66 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assert, assertEquals } from "../../../testing/asserts.ts"; +const { test } = Deno; +import { generate, validate } from "../../v5.ts"; +const NAMESPACE = "1b671a64-40d5-491e-99b0-da01ff1f3341"; +test({ + name: "[UUID] test_uuid_v5", + fn(): void { + const u = generate({ value: "", namespace: NAMESPACE }); + assertEquals(typeof u, "string", "returns a string"); + assert(u !== "", "return string is not empty"); + }, +}); + +test({ + name: "[UUID] test_uuid_v5_format", + fn(): void { + for (let i = 0; i < 10000; i++) { + const u = generate({ value: String(i), namespace: NAMESPACE }) as string; + assert(validate(u), `${u} is not a valid uuid v5`); + } + }, +}); + +test({ + name: "[UUID] test_uuid_v5_option", + fn(): void { + const v5Options = { + value: "Hello, World", + namespace: NAMESPACE, + }; + const u = generate(v5Options); + assertEquals(u, "4b4f2adc-5b27-57b5-8e3a-c4c4bcf94f05"); + }, +}); + +test({ + name: "[UUID] test_uuid_v5_buf_offset", + fn(): void { + const buf = [ + 75, + 79, + 42, + 220, + 91, + 39, + 87, + 181, + 142, + 58, + 196, + 196, + 188, + 249, + 79, + 5, + ]; + const origin = JSON.parse(JSON.stringify(buf)); + generate({ value: "Hello, World", namespace: NAMESPACE }, buf); + assertEquals(origin, buf); + + generate({ value: "Hello, World", namespace: NAMESPACE }, buf, 3); + assertEquals(origin.slice(0, 3), buf.slice(0, 3)); + assertEquals(origin, buf.slice(3)); + }, +}); diff --git a/std/uuid/tests/v5/validate.ts b/std/uuid/tests/v5/validate.ts new file mode 100644 index 000000000..888acb1c5 --- /dev/null +++ b/std/uuid/tests/v5/validate.ts @@ -0,0 +1,19 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assert } from "../../../testing/asserts.ts"; +import { generate, validate } from "../../v5.ts"; + +Deno.test({ + name: "[UUID] is_valid_uuid_v5", + fn(): void { + const u = generate({ + value: "Hello, World", + namespace: "1b671a64-40d5-491e-99b0-da01ff1f3341", + }) as string; + const t = "4b4f2adc-5b27-57b5-8e3a-c4c4bcf94f05"; + const n = "4b4f2adc-5b27-17b5-8e3a-c4c4bcf94f05"; + + assert(validate(u), `generated ${u} should be valid`); + assert(validate(t), `${t} should be valid`); + assert(!validate(n), `${n} should not be valid`); + }, +}); diff --git a/std/uuid/v5.ts b/std/uuid/v5.ts new file mode 100644 index 000000000..1216762dd --- /dev/null +++ b/std/uuid/v5.ts @@ -0,0 +1,52 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { + bytesToUuid, + createBuffer, + stringToBytes, + uuidToBytes, +} from "./_common.ts"; +import { Sha1 } from "../util/sha1.ts"; +import { isString } from "../node/util.ts"; +import { assert } from "../testing/asserts.ts"; + +const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; + +export function validate(id: string): boolean { + return UUID_RE.test(id); +} + +interface V5Options { + value: string | number[]; + namespace: string | number[]; +} + +export function generate( + options: V5Options, + buf?: number[], + offset?: number +): string | number[] { + const i = (buf && offset) || 0; + + let { value, namespace } = options; + if (isString(value)) value = stringToBytes(value as string); + if (isString(namespace)) namespace = uuidToBytes(namespace as string); + assert( + namespace.length === 16, + "namespace must be uuid string or an Array of 16 byte values" + ); + + const content = (namespace as number[]).concat(value as number[]); + const bytes = new Sha1().update(createBuffer(content)).digest(); + + bytes[6] = (bytes[6] & 0x0f) | 0x50; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + + if (buf) { + for (let idx = 0; idx < 16; ++idx) { + buf[i + idx] = bytes[idx]; + } + } + + return buf || bytesToUuid(bytes); +} diff --git a/std/ws/mod.ts b/std/ws/mod.ts index 87084312f..9632fbbfb 100644 --- a/std/ws/mod.ts +++ b/std/ws/mod.ts @@ -4,7 +4,7 @@ import { decode, encode } from "../encoding/utf8.ts"; import { hasOwnProperty } from "../util/has_own_property.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts"; -import { Sha1 } from "./sha1.ts"; +import { Sha1 } from "../util/sha1.ts"; import { writeResponse } from "../http/io.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { Deferred, deferred } from "../util/async.ts"; diff --git a/std/ws/sha1.ts b/std/ws/sha1.ts deleted file mode 100644 index fc86881f8..000000000 --- a/std/ws/sha1.ts +++ /dev/null @@ -1,374 +0,0 @@ -/* - * [js-sha1]{@link https://github.com/emn178/js-sha1} - * - * @version 0.6.0 - * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 - * @license MIT - */ -/*jslint bitwise: true */ - -const HEX_CHARS = "0123456789abcdef".split(""); -const EXTRA = Uint32Array.of(-2147483648, 8388608, 32768, 128); -const SHIFT = Uint32Array.of(24, 16, 8, 0); - -const blocks = new Uint32Array(80); - -export class Sha1 { - private _blocks: Uint32Array; - private _block: number; - private _start: number; - private _bytes: number; - private _hBytes: number; - private _finalized: boolean; - private _hashed: boolean; - - private _h0 = 0x67452301; - private _h1 = 0xefcdab89; - private _h2 = 0x98badcfe; - private _h3 = 0x10325476; - private _h4 = 0xc3d2e1f0; - private _lastByteIndex = 0; - - constructor(sharedMemory = false) { - if (sharedMemory) { - this._blocks = blocks.fill(0, 0, 17); - } else { - this._blocks = new Uint32Array(80); - } - - this._h0 = 0x67452301; - this._h1 = 0xefcdab89; - this._h2 = 0x98badcfe; - this._h3 = 0x10325476; - this._h4 = 0xc3d2e1f0; - - this._block = this._start = this._bytes = this._hBytes = 0; - this._finalized = this._hashed = false; - } - - update(data: string | ArrayBuffer | ArrayBufferView): void { - if (this._finalized) { - return; - } - let notString = true; - let message; - if (data instanceof ArrayBuffer) { - message = new Uint8Array(data); - } else if (ArrayBuffer.isView(data)) { - message = new Uint8Array(data.buffer); - } else { - notString = false; - message = String(data); - } - let code; - let index = 0; - let i; - const start = this._start; - const length = message.length || 0; - const blocks = this._blocks; - - while (index < length) { - if (this._hashed) { - this._hashed = false; - blocks[0] = this._block; - blocks.fill(0, 1, 17); - } - - if (notString) { - for (i = start; index < length && i < 64; ++index) { - blocks[i >> 2] |= (message[index] as number) << SHIFT[i++ & 3]; - } - } else { - for (i = start; index < length && i < 64; ++index) { - code = (message as string).charCodeAt(index); - if (code < 0x80) { - blocks[i >> 2] |= code << SHIFT[i++ & 3]; - } else if (code < 0x800) { - blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else if (code < 0xd800 || code >= 0xe000) { - blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else { - code = - 0x10000 + - (((code & 0x3ff) << 10) | - ((message as string).charCodeAt(++index) & 0x3ff)); - blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } - } - } - - this._lastByteIndex = i; - this._bytes += i - start; - if (i >= 64) { - this._block = blocks[16]; - this._start = i - 64; - this.hash(); - this._hashed = true; - } else { - this._start = i; - } - } - if (this._bytes > 4294967295) { - this._hBytes += (this._bytes / 4294967296) >>> 0; - this._bytes = this._bytes >>> 0; - } - } - - finalize(): void { - if (this._finalized) { - return; - } - this._finalized = true; - const blocks = this._blocks; - const i = this._lastByteIndex; - blocks[16] = this._block; - blocks[i >> 2] |= EXTRA[i & 3]; - this._block = blocks[16]; - if (i >= 56) { - if (!this._hashed) { - this.hash(); - } - blocks[0] = this._block; - blocks.fill(0, 1, 17); - } - blocks[14] = (this._hBytes << 3) | (this._bytes >>> 29); - blocks[15] = this._bytes << 3; - this.hash(); - } - - hash(): void { - let a = this._h0; - let b = this._h1; - let c = this._h2; - let d = this._h3; - let e = this._h4; - let f, j, t; - const blocks = this._blocks; - - for (j = 16; j < 80; ++j) { - t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16]; - blocks[j] = (t << 1) | (t >>> 31); - } - - for (j = 0; j < 20; j += 5) { - f = (b & c) | (~b & d); - t = (a << 5) | (a >>> 27); - e = (t + f + e + 1518500249 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = (a & b) | (~a & c); - t = (e << 5) | (e >>> 27); - d = (t + f + d + 1518500249 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = (e & a) | (~e & b); - t = (d << 5) | (d >>> 27); - c = (t + f + c + 1518500249 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = (d & e) | (~d & a); - t = (c << 5) | (c >>> 27); - b = (t + f + b + 1518500249 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = (c & d) | (~c & e); - t = (b << 5) | (b >>> 27); - a = (t + f + a + 1518500249 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - for (; j < 40; j += 5) { - f = b ^ c ^ d; - t = (a << 5) | (a >>> 27); - e = (t + f + e + 1859775393 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = a ^ b ^ c; - t = (e << 5) | (e >>> 27); - d = (t + f + d + 1859775393 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = e ^ a ^ b; - t = (d << 5) | (d >>> 27); - c = (t + f + c + 1859775393 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = d ^ e ^ a; - t = (c << 5) | (c >>> 27); - b = (t + f + b + 1859775393 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = c ^ d ^ e; - t = (b << 5) | (b >>> 27); - a = (t + f + a + 1859775393 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - for (; j < 60; j += 5) { - f = (b & c) | (b & d) | (c & d); - t = (a << 5) | (a >>> 27); - e = (t + f + e - 1894007588 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = (a & b) | (a & c) | (b & c); - t = (e << 5) | (e >>> 27); - d = (t + f + d - 1894007588 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = (e & a) | (e & b) | (a & b); - t = (d << 5) | (d >>> 27); - c = (t + f + c - 1894007588 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = (d & e) | (d & a) | (e & a); - t = (c << 5) | (c >>> 27); - b = (t + f + b - 1894007588 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = (c & d) | (c & e) | (d & e); - t = (b << 5) | (b >>> 27); - a = (t + f + a - 1894007588 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - for (; j < 80; j += 5) { - f = b ^ c ^ d; - t = (a << 5) | (a >>> 27); - e = (t + f + e - 899497514 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = a ^ b ^ c; - t = (e << 5) | (e >>> 27); - d = (t + f + d - 899497514 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = e ^ a ^ b; - t = (d << 5) | (d >>> 27); - c = (t + f + c - 899497514 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = d ^ e ^ a; - t = (c << 5) | (c >>> 27); - b = (t + f + b - 899497514 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = c ^ d ^ e; - t = (b << 5) | (b >>> 27); - a = (t + f + a - 899497514 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - this._h0 = (this._h0 + a) >>> 0; - this._h1 = (this._h1 + b) >>> 0; - this._h2 = (this._h2 + c) >>> 0; - this._h3 = (this._h3 + d) >>> 0; - this._h4 = (this._h4 + e) >>> 0; - } - - hex(): string { - this.finalize(); - - const h0 = this._h0; - const h1 = this._h1; - const h2 = this._h2; - const h3 = this._h3; - const h4 = this._h4; - - return ( - HEX_CHARS[(h0 >> 28) & 0x0f] + - HEX_CHARS[(h0 >> 24) & 0x0f] + - HEX_CHARS[(h0 >> 20) & 0x0f] + - HEX_CHARS[(h0 >> 16) & 0x0f] + - HEX_CHARS[(h0 >> 12) & 0x0f] + - HEX_CHARS[(h0 >> 8) & 0x0f] + - HEX_CHARS[(h0 >> 4) & 0x0f] + - HEX_CHARS[h0 & 0x0f] + - HEX_CHARS[(h1 >> 28) & 0x0f] + - HEX_CHARS[(h1 >> 24) & 0x0f] + - HEX_CHARS[(h1 >> 20) & 0x0f] + - HEX_CHARS[(h1 >> 16) & 0x0f] + - HEX_CHARS[(h1 >> 12) & 0x0f] + - HEX_CHARS[(h1 >> 8) & 0x0f] + - HEX_CHARS[(h1 >> 4) & 0x0f] + - HEX_CHARS[h1 & 0x0f] + - HEX_CHARS[(h2 >> 28) & 0x0f] + - HEX_CHARS[(h2 >> 24) & 0x0f] + - HEX_CHARS[(h2 >> 20) & 0x0f] + - HEX_CHARS[(h2 >> 16) & 0x0f] + - HEX_CHARS[(h2 >> 12) & 0x0f] + - HEX_CHARS[(h2 >> 8) & 0x0f] + - HEX_CHARS[(h2 >> 4) & 0x0f] + - HEX_CHARS[h2 & 0x0f] + - HEX_CHARS[(h3 >> 28) & 0x0f] + - HEX_CHARS[(h3 >> 24) & 0x0f] + - HEX_CHARS[(h3 >> 20) & 0x0f] + - HEX_CHARS[(h3 >> 16) & 0x0f] + - HEX_CHARS[(h3 >> 12) & 0x0f] + - HEX_CHARS[(h3 >> 8) & 0x0f] + - HEX_CHARS[(h3 >> 4) & 0x0f] + - HEX_CHARS[h3 & 0x0f] + - HEX_CHARS[(h4 >> 28) & 0x0f] + - HEX_CHARS[(h4 >> 24) & 0x0f] + - HEX_CHARS[(h4 >> 20) & 0x0f] + - HEX_CHARS[(h4 >> 16) & 0x0f] + - HEX_CHARS[(h4 >> 12) & 0x0f] + - HEX_CHARS[(h4 >> 8) & 0x0f] + - HEX_CHARS[(h4 >> 4) & 0x0f] + - HEX_CHARS[h4 & 0x0f] - ); - } - - toString(): string { - return this.hex(); - } - - digest(): number[] { - this.finalize(); - - const h0 = this._h0; - const h1 = this._h1; - const h2 = this._h2; - const h3 = this._h3; - const h4 = this._h4; - - return [ - (h0 >> 24) & 0xff, - (h0 >> 16) & 0xff, - (h0 >> 8) & 0xff, - h0 & 0xff, - (h1 >> 24) & 0xff, - (h1 >> 16) & 0xff, - (h1 >> 8) & 0xff, - h1 & 0xff, - (h2 >> 24) & 0xff, - (h2 >> 16) & 0xff, - (h2 >> 8) & 0xff, - h2 & 0xff, - (h3 >> 24) & 0xff, - (h3 >> 16) & 0xff, - (h3 >> 8) & 0xff, - h3 & 0xff, - (h4 >> 24) & 0xff, - (h4 >> 16) & 0xff, - (h4 >> 8) & 0xff, - h4 & 0xff, - ]; - } - - array(): number[] { - return this.digest(); - } - - arrayBuffer(): ArrayBuffer { - this.finalize(); - return Uint32Array.of(this._h0, this._h1, this._h2, this._h3, this._h4) - .buffer; - } -} diff --git a/std/ws/sha1_test.ts b/std/ws/sha1_test.ts deleted file mode 100644 index 8830173a4..000000000 --- a/std/ws/sha1_test.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; -import { assertEquals } from "../testing/asserts.ts"; -import { Sha1 } from "./sha1.ts"; - -test("[ws/sha] test1", () => { - const sha1 = new Sha1(); - sha1.update("abcde"); - assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); -}); - -test("[ws/sha] testWithArray", () => { - const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); - const sha1 = new Sha1(); - sha1.update(data); - assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); -}); - -test("[ws/sha] testSha1WithBuffer", () => { - const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); - const sha1 = new Sha1(); - sha1.update(data.buffer); - assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); -}); -- cgit v1.2.3