• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2/*
3 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.SHA1Hash = exports.createSha1 = void 0;
18const compat_1 = require("#koalaui/compat");
19const K = [
20    (0x5a827999 | 0),
21    (0x6ed9eba1 | 0),
22    (0x8f1bbcdc | 0),
23    (0xca62c1d6 | 0),
24];
25const inputBytes = 64;
26const inputWords = inputBytes / 4;
27const highIndex = inputWords - 2;
28const lowIndex = inputWords - 1;
29const workWords = 80;
30const allocBytes = 80;
31const allocWords = allocBytes / 4;
32const allocTotal = allocBytes * 100;
33function createSha1() {
34    return new SHA1Hash();
35}
36exports.createSha1 = createSha1;
37class SHA1Hash {
38    constructor() {
39        this.A = (0x67452301 | 0);
40        this.B = (0xefcdab89 | 0);
41        this.C = (0x98badcfe | 0);
42        this.D = (0x10325476 | 0);
43        this.E = (0xc3d2e1f0 | 0);
44        this._size = 0;
45        this._sp = 0; // surrogate pair
46        if (!sharedBuffer || sharedOffset >= allocTotal) {
47            sharedBuffer = new ArrayBuffer(allocTotal);
48            sharedOffset = 0;
49        }
50        this._byte = new Uint8Array(sharedBuffer, sharedOffset, allocBytes);
51        this._word = new Int32Array(sharedBuffer, sharedOffset, allocWords);
52        sharedOffset += allocBytes;
53    }
54    updateString(data, encoding) {
55        return this._utf8(data);
56    }
57    updateInt32(data) {
58        const buffer = new Int32Array(1);
59        buffer[0] = data;
60        return this.update(buffer);
61    }
62    update(data) {
63        if (data == null) {
64            throw new TypeError("SHA1Hash expected non-null data: ");
65        }
66        let byteOffset = 0;
67        let length = 0;
68        let buffer = undefined;
69        // TODO: an attempt to wrie this in a generic form causes
70        // es2panda to segfault.
71        if (data instanceof Int32Array) {
72            byteOffset = data.byteOffset;
73            length = data.byteLength;
74            buffer = data.buffer;
75        }
76        else if (data instanceof Uint32Array) {
77            byteOffset = data.byteOffset;
78            length = data.byteLength;
79            buffer = data.buffer;
80        }
81        else if (data instanceof Float32Array) {
82            byteOffset = data.byteOffset;
83            length = data.byteLength;
84            buffer = data.buffer;
85        }
86        else if (data instanceof Uint8Array) {
87            byteOffset = data.byteOffset;
88            length = data.byteLength;
89            buffer = data.buffer;
90        }
91        let blocks = ((length / inputBytes) | 0);
92        let offset = 0;
93        // longer than 1 block
94        if ((blocks != 0) && !(byteOffset & 3) && !(this._size % inputBytes)) {
95            const block = new Int32Array(buffer, byteOffset, blocks * inputWords);
96            while (blocks--) {
97                this._int32(block, offset >> 2);
98                offset += inputBytes;
99            }
100            this._size += offset;
101        }
102        // data: TypedArray | DataView
103        const BYTES_PER_ELEMENT = data.BYTES_PER_ELEMENT;
104        if ((BYTES_PER_ELEMENT != 1) && buffer != undefined) {
105            const rest = new Uint8Array(buffer, byteOffset + offset, length - offset);
106            return this._uint8(rest);
107        }
108        // no more bytes
109        if (offset == length)
110            return this;
111        return this._uint8(new Uint8Array(buffer), offset);
112    }
113    _uint8(data, offset) {
114        const _byte = this._byte;
115        const _word = this._word;
116        const length = data.length;
117        offset = ((offset !== null && offset !== void 0 ? offset : 0) | 0);
118        while (offset < length) {
119            const start = this._size % inputBytes;
120            let index = start;
121            while (offset < length && index < inputBytes) {
122                _byte[index++] = data[offset++];
123            }
124            if (index >= inputBytes) {
125                this._int32(_word);
126            }
127            this._size += index - start;
128        }
129        return this;
130    }
131    _utf8(text) {
132        const _byte = this._byte;
133        const _word = this._word;
134        const length = text.length;
135        let surrogate = this._sp;
136        for (let offset = 0; offset < length;) {
137            const start = this._size % inputBytes;
138            let index = start;
139            while (offset < length && index < inputBytes) {
140                let code = text.charCodeAt(offset++) | 0;
141                if (code < 0x80) {
142                    // ASCII characters
143                    _byte[index++] = code;
144                }
145                else if (code < 0x800) {
146                    // 2 bytes
147                    _byte[index++] = 0xC0 | (code >>> 6);
148                    _byte[index++] = 0x80 | (code & 0x3F);
149                }
150                else if (code < 0xD800 || code > 0xDFFF) {
151                    // 3 bytes
152                    _byte[index++] = 0xE0 | (code >>> 12);
153                    _byte[index++] = 0x80 | ((code >>> 6) & 0x3F);
154                    _byte[index++] = 0x80 | (code & 0x3F);
155                }
156                else if (surrogate) {
157                    // 4 bytes - surrogate pair
158                    code = ((surrogate & 0x3FF) << 10) + (code & 0x3FF) + 0x10000;
159                    _byte[index++] = 0xF0 | (code >>> 18);
160                    _byte[index++] = 0x80 | ((code >>> 12) & 0x3F);
161                    _byte[index++] = 0x80 | ((code >>> 6) & 0x3F);
162                    _byte[index++] = 0x80 | (code & 0x3F);
163                    surrogate = 0;
164                }
165                else {
166                    surrogate = code;
167                }
168            }
169            if (index >= inputBytes) {
170                this._int32(_word);
171                _word[0] = _word[inputWords];
172            }
173            this._size += index - start;
174        }
175        this._sp = surrogate;
176        return this;
177    }
178    _int32(data, offset) {
179        let A = this.A;
180        let B = this.B;
181        let C = this.C;
182        let D = this.D;
183        let E = this.E;
184        let i = 0;
185        offset = ((offset !== null && offset !== void 0 ? offset : 0) | 0);
186        while (i < inputWords) {
187            W[i++] = swap32(data[offset++]);
188        }
189        for (i = inputWords; i < workWords; i++) {
190            W[i] = rotate1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]);
191        }
192        for (i = 0; i < workWords; i++) {
193            const S = (i / 20) | 0;
194            const T = (rotate5(A) + ft(S, B, C, D) + E + W[i] + K[S]) | 0;
195            E = D;
196            D = C;
197            C = rotate30(B);
198            B = A;
199            A = T;
200        }
201        this.A = (A + this.A) | 0;
202        this.B = (B + this.B) | 0;
203        this.C = (C + this.C) | 0;
204        this.D = (D + this.D) | 0;
205        this.E = (E + this.E) | 0;
206    }
207    // digest(): Uint8Array
208    // digest(encoding: string): string
209    digest(encoding) {
210        const _byte = this._byte;
211        const _word = this._word;
212        let i = (this._size % inputBytes) | 0;
213        _byte[i++] = 0x80;
214        // pad 0 for current word
215        while (i & 3) {
216            _byte[i++] = 0;
217        }
218        i >>= 2;
219        if (i > highIndex) {
220            while (i < inputWords) {
221                _word[i++] = 0;
222            }
223            i = 0;
224            this._int32(_word);
225        }
226        // pad 0 for rest words
227        while (i < inputWords) {
228            _word[i++] = 0;
229        }
230        // input size
231        const bits64 = this._size * 8;
232        const low32 = ((bits64 & 0xffffffff) >>> 0);
233        const high32 = ((bits64 - low32) / 0x100000000);
234        if (high32)
235            _word[highIndex] = swap32(high32);
236        if (low32)
237            _word[lowIndex] = swap32(low32);
238        this._int32(_word);
239        return (encoding === "hex") ? this._hex() : this._bin();
240    }
241    _hex() {
242        let A = this.A;
243        let B = this.B;
244        let C = this.C;
245        let D = this.D;
246        let E = this.E;
247        return hex32Str(A, B, C, D, E);
248    }
249    _bin() {
250        let A = this.A;
251        let B = this.B;
252        let C = this.C;
253        let D = this.D;
254        let E = this.E;
255        const _byte = this._byte;
256        const _word = this._word;
257        _word[0] = swap32(A);
258        _word[1] = swap32(B);
259        _word[2] = swap32(C);
260        _word[3] = swap32(D);
261        _word[4] = swap32(E);
262        return _byte.slice(0, 20);
263    }
264}
265exports.SHA1Hash = SHA1Hash;
266const W = new Int32Array(workWords);
267let sharedBuffer;
268let sharedOffset = 0;
269const swapLE = ((c) => (((c << 24) & 0xff000000) | ((c << 8) & 0xff0000) | ((c >> 8) & 0xff00) | ((c >> 24) & 0xff)));
270const swapBE = ((c) => c);
271const swap32 = isBE() ? swapBE : swapLE;
272const rotate1 = (num) => (num << 1) | (num >>> 31);
273const rotate5 = (num) => (num << 5) | (num >>> 27);
274const rotate30 = (num) => (num << 30) | (num >>> 2);
275function isBE() {
276    let a16 = new Uint16Array(1);
277    a16[0] = 0xFEFF;
278    let a8 = new Uint8Array(a16.buffer);
279    return a8[0] == 0xFE; // BOM
280}
281function ft(s, b, c, d) {
282    if (s == 0)
283        return (b & c) | ((~b) & d);
284    if (s == 2)
285        return (b & c) | (b & d) | (c & d);
286    return b ^ c ^ d;
287}
288const hex32Decoder = new compat_1.CustomTextDecoder();
289const hex32DecodeBuffer = new Uint8Array(40);
290function hex32Str(A, B, C, D, E) {
291    writeIntAsHexUTF8(A, hex32DecodeBuffer, 0);
292    writeIntAsHexUTF8(B, hex32DecodeBuffer, 8);
293    writeIntAsHexUTF8(C, hex32DecodeBuffer, 16);
294    writeIntAsHexUTF8(D, hex32DecodeBuffer, 24);
295    writeIntAsHexUTF8(E, hex32DecodeBuffer, 32);
296    return hex32Decoder.decode(hex32DecodeBuffer);
297}
298function writeIntAsHexUTF8(value, buffer, byteOffset) {
299    buffer[byteOffset++] = nibbleToHexCode((value >> 28) & 0xF);
300    buffer[byteOffset++] = nibbleToHexCode((value >> 24) & 0xF);
301    buffer[byteOffset++] = nibbleToHexCode((value >> 20) & 0xF);
302    buffer[byteOffset++] = nibbleToHexCode((value >> 16) & 0xF);
303    buffer[byteOffset++] = nibbleToHexCode((value >> 12) & 0xF);
304    buffer[byteOffset++] = nibbleToHexCode((value >> 8) & 0xF);
305    buffer[byteOffset++] = nibbleToHexCode((value >> 4) & 0xF);
306    buffer[byteOffset++] = nibbleToHexCode((value >> 0) & 0xF);
307}
308function nibbleToHexCode(nibble) {
309    return nibble > 9 ? nibble + 87 : nibble + 48;
310}
311//# sourceMappingURL=sha1.js.map