forked from github-mirror/twing
Replace create-hash
with own implementation
This commit is contained in:
parent
ecb54b93fd
commit
2341424658
src/main/lib
@ -1,13 +1,13 @@
|
||||
import {createSynchronousTemplate, TwingSynchronousTemplate} from "../../../template";
|
||||
import {TwingSynchronousCallable} from "../../../callable-wrapper";
|
||||
import createHash from "create-hash";
|
||||
import {createSource} from "../../../source";
|
||||
import {TwingSynchronousExecutionContext} from "../../../execution-context";
|
||||
import { TwingSynchronousCallable } from "../../../callable-wrapper";
|
||||
import { TwingSynchronousExecutionContext } from "../../../execution-context";
|
||||
import sha1 from "../../../helpers/sha1";
|
||||
import { createSource } from "../../../source";
|
||||
import { createSynchronousTemplate, TwingSynchronousTemplate } from "../../../template";
|
||||
|
||||
const getAST = (executionContext: TwingSynchronousExecutionContext, code: string, name: string | null) => {
|
||||
const {environment} = executionContext;
|
||||
|
||||
const hash: string = createHash("sha256").update(code).digest("hex").toString();
|
||||
const hash = sha1(code);
|
||||
|
||||
if (name !== null) {
|
||||
name = `${name} (string template ${hash})`;
|
||||
|
93
src/main/lib/helpers/sha1.ts
Normal file
93
src/main/lib/helpers/sha1.ts
Normal file
@ -0,0 +1,93 @@
|
||||
export default function sha1(message: string): string {
|
||||
const bytes = new TextEncoder().encode(message);
|
||||
|
||||
// Constants
|
||||
const H: [number, number, number, number, number] = [
|
||||
0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
|
||||
];
|
||||
|
||||
// Pad the message
|
||||
const paddedLength = ((bytes.length + 9 + 63) & ~63) - bytes.length;
|
||||
const paddedMessage = new Uint8Array(bytes.length + paddedLength + 8);
|
||||
paddedMessage.set(bytes);
|
||||
paddedMessage[bytes.length] = 0x80;
|
||||
|
||||
const bitLength = bytes.length * 8;
|
||||
new DataView(paddedMessage.buffer).setUint32(
|
||||
paddedMessage.length - 4,
|
||||
bitLength
|
||||
);
|
||||
|
||||
// Process the message in 512-bit chunks
|
||||
const W = new Uint32Array(80);
|
||||
|
||||
for (let i = 0; i < paddedMessage.length; i += 64) {
|
||||
const chunk = paddedMessage.subarray(i, i + 64);
|
||||
const chunkView = new DataView(
|
||||
chunk.buffer,
|
||||
chunk.byteOffset,
|
||||
chunk.byteLength
|
||||
);
|
||||
|
||||
// Prepare the message schedule
|
||||
for (let t = 0; t < 16; t++) {
|
||||
W[t] = chunkView.getUint32(t * 4);
|
||||
}
|
||||
|
||||
for (let t = 16; t < 80; t++) {
|
||||
W[t] = rotateLeft(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
|
||||
}
|
||||
|
||||
// Initialize working variables
|
||||
let [a, b, c, d, e] = H;
|
||||
|
||||
// Main loop
|
||||
for (let t = 0; t < 80; t++) {
|
||||
const K =
|
||||
t < 20
|
||||
? 0x5a827999
|
||||
: t < 40
|
||||
? 0x6ed9eba1
|
||||
: t < 60
|
||||
? 0x8f1bbcdc
|
||||
: 0xca62c1d6;
|
||||
|
||||
const f =
|
||||
t < 20
|
||||
? (b & c) | (~b & d)
|
||||
: t < 40
|
||||
? b ^ c ^ d
|
||||
: t < 60
|
||||
? (b & c) | (b & d) | (c & d)
|
||||
: b ^ c ^ d;
|
||||
|
||||
const temp = (rotateLeft(a, 5) + f + e + K + W[t]) >>> 0;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rotateLeft(b, 30);
|
||||
b = a;
|
||||
a = temp;
|
||||
}
|
||||
|
||||
// Add the working variables back to H
|
||||
H[0] = (H[0] + a) >>> 0;
|
||||
H[1] = (H[1] + b) >>> 0;
|
||||
H[2] = (H[2] + c) >>> 0;
|
||||
H[3] = (H[3] + d) >>> 0;
|
||||
H[4] = (H[4] + e) >>> 0;
|
||||
}
|
||||
|
||||
const dv = new DataView(new ArrayBuffer(20))
|
||||
for (let i = 0; i < H.length; i++) {
|
||||
dv.setUint32(i * 4, H[i], false)
|
||||
}
|
||||
|
||||
let result = ''
|
||||
for (let i = 0; i < dv.byteLength; i++) {
|
||||
result += dv.getUint8(i).toString(16).padStart(2, '0')
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const rotateLeft = (n: number, s: number): number =>
|
||||
(n << s) | (n >>> (32 - s));
|
Loading…
x
Reference in New Issue
Block a user