@stryke/hash 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,27 +16,27 @@ const f = Object.freeze({
16
16
  excludeValues: void 0,
17
17
  replacer: void 0
18
18
  });
19
- function hashObject(t, u) {
20
- u = u ? {
19
+ function hashObject(t, o) {
20
+ o ? o = {
21
21
  ...f,
22
- ...u
23
- } : f;
24
- const o = p(u);
25
- return o.dispatch(t), o.toString();
22
+ ...o
23
+ } : o = f;
24
+ const u = p(o);
25
+ return u.dispatch(t), u.toString();
26
26
  }
27
27
  const b = Object.freeze(["prototype", "__proto__", "constructor"]);
28
28
  function p(t) {
29
- let u = "",
30
- o = new Map();
29
+ let o = "",
30
+ u = new Map();
31
31
  const r = e => {
32
- u += e;
32
+ o += e;
33
33
  };
34
34
  return {
35
35
  toString() {
36
- return u;
36
+ return o;
37
37
  },
38
38
  getContext() {
39
- return o;
39
+ return u;
40
40
  },
41
41
  dispatch(e) {
42
42
  return t.replacer && (e = t.replacer(e)), this[e === null ? "null" : typeof e](e);
@@ -46,9 +46,9 @@ function p(t) {
46
46
  const n = Object.prototype.toString.call(e);
47
47
  let a = "";
48
48
  const y = n.length;
49
- a = y < 10 ? "unknown:[" + n + "]" : n.slice(8, y - 1), a = a.toLowerCase();
50
- let l = o.get(e);
51
- if (l === void 0) o.set(e, o.size);else return this.dispatch("[CIRCULAR:" + l + "]");
49
+ y < 10 ? a = "unknown:[" + n + "]" : a = n.slice(8, y - 1), a = a.toLowerCase();
50
+ let l = null;
51
+ if ((l = u.get(e)) === void 0) u.set(e, u.size);else return this.dispatch("[CIRCULAR:" + l + "]");
52
52
  if (typeof Buffer < "u" && Buffer.isBuffer && Buffer.isBuffer(e)) return r("buffer:"), r(e.toString("utf8"));
53
53
  if (a !== "object" && a !== "function" && a !== "asyncfunction") this[a] ? this[a](e) : t.ignoreUnknown || this.unknown(e, a);else {
54
54
  let i = Object.keys(e);
@@ -63,7 +63,7 @@ function p(t) {
63
63
  }
64
64
  },
65
65
  array(e, n) {
66
- if (n = n ?? t.unorderedArrays !== !1, r("array:" + e.length + ":"), !n || e.length <= 1) {
66
+ if (n = n === void 0 ? t.unorderedArrays !== !1 : n, r("array:" + e.length + ":"), !n || e.length <= 1) {
67
67
  for (const l of e) this.dispatch(l);
68
68
  return;
69
69
  }
@@ -74,7 +74,7 @@ function p(t) {
74
74
  for (const [c, d] of i.getContext()) a.set(c, d);
75
75
  return i.toString();
76
76
  });
77
- return o = a, y.sort(), this.array(y, !1);
77
+ return u = a, y.sort(), this.array(y, !1);
78
78
  },
79
79
  date(e) {
80
80
  return r("date:" + e.toJSON());
@@ -83,7 +83,7 @@ function p(t) {
83
83
  return r("symbol:" + e.toString());
84
84
  },
85
85
  unknown(e, n) {
86
- if (r(n), !!e && (r(":"), e && typeof e.entries == "function")) return this.array([...e.entries()], !0);
86
+ if (r(n), !!e && (r(":"), e && typeof e.entries == "function")) return this.array(Array.from(e.entries()), !0);
87
87
  },
88
88
  error(e) {
89
89
  return r("error:" + e.toString());
@@ -1,71 +1,71 @@
1
1
  export interface HashObjectOptions {
2
2
  /**
3
3
  * Function to determine if a key should be excluded from hashing.
4
- *
4
+ * @optional
5
5
  * @param key - The key to check for exclusion.
6
- * @returns Returns true to exclude the key from hashing.
6
+ * @returns {boolean} - Returns true to exclude the key from hashing.
7
7
  */
8
8
  excludeKeys?: ((key: string) => boolean) | undefined;
9
9
  /**
10
10
  * Specifies whether to exclude values from hashing, so that only the object keys are hashed.
11
- *
11
+ * @optional
12
12
  */
13
13
  excludeValues?: boolean | undefined;
14
14
  /**
15
15
  * Specifies whether to ignore objects of unknown type (not directly serialisable) when hashing.
16
- *
17
- * @defaultValue false
16
+ * @optional
17
+ * @default false
18
18
  */
19
19
  ignoreUnknown?: boolean | undefined;
20
20
  /**
21
- * A function that replaces values before they are hashed, which can be used to customize the hashing process.
22
- *
21
+ * A function that replaces values before they are hashed, which can be used to customise the hashing process.
22
+ * @optional
23
23
  * @param value - The current value to be hashed.
24
- * @returns The value to use for hashing instead.
24
+ * @returns {any} - The value to use for hashing instead.
25
25
  */
26
26
  replacer?: ((value: any) => any) | undefined;
27
27
  /**
28
28
  * Specifies whether the 'name' property of functions should be taken into account when hashing.
29
- *
30
- * @defaultValue false
29
+ * @optional
30
+ * @default false
31
31
  */
32
32
  respectFunctionNames?: boolean | undefined;
33
33
  /**
34
34
  * Specifies whether properties of functions should be taken into account when hashing.
35
- *
36
- * @defaultValue false
35
+ * @optional
36
+ * @default false
37
37
  */
38
38
  respectFunctionProperties?: boolean | undefined;
39
39
  /**
40
40
  * Specifies whether to include type-specific properties such as prototype or constructor in the hash to distinguish between types.
41
- *
42
- * @defaultValue false
41
+ * @optional
42
+ * @default false
43
43
  */
44
44
  respectType?: boolean | undefined;
45
45
  /**
46
46
  * Specifies whether arrays should be sorted before hashing to ensure consistent order.
47
- *
48
- * @defaultValue false
47
+ * @optional
48
+ * @default false
49
49
  */
50
50
  unorderedArrays?: boolean | undefined;
51
51
  /**
52
52
  * Specifies whether Set and Map instances should be sorted by key before hashing to ensure consistent order.
53
- *
54
- * @defaultValue true
53
+ * @optional
54
+ * @default true
55
55
  */
56
56
  unorderedObjects?: boolean | undefined;
57
57
  /**
58
58
  * Specifies whether the elements of `Set' and keys of `Map' should be sorted before hashing to ensure consistent order.
59
- *
60
- * @defaultValue false
59
+ * @optional
60
+ * @default false
61
61
  */
62
62
  unorderedSets?: boolean | undefined;
63
63
  }
64
64
  /**
65
65
  * Serialize any JS value into a stable, hashable string
66
- *
67
- * @param object - value to hash
68
- * @param options - hashing options. See {@link HashObjectOptions}.
69
- * @returns serialized value
66
+ * @param {object} object value to hash
67
+ * @param {HashObjectOptions} options hashing options. See {@link HashObjectOptions}.
68
+ * @return {string} serialized value
69
+ * @api public
70
70
  */
71
71
  export declare function hashObject(object: any, options?: HashObjectOptions): string;
@@ -1,3 +1,3 @@
1
- const f=Object.freeze({ignoreUnknown:!1,respectType:!1,respectFunctionNames:!1,respectFunctionProperties:!1,unorderedObjects:!0,unorderedArrays:!1,unorderedSets:!1,excludeKeys:void 0,excludeValues:void 0,replacer:void 0});export function hashObject(t,u){u=u?{...f,...u}:f;const o=p(u);return o.dispatch(t),o.toString()}const b=Object.freeze(["prototype","__proto__","constructor"]);function p(t){let u="",o=new Map;const r=e=>{u+=e};return{toString(){return u},getContext(){return o},dispatch(e){return t.replacer&&(e=t.replacer(e)),this[e===null?"null":typeof e](e)},object(e){if(e&&typeof e.toJSON=="function")return this.object(e.toJSON());const n=Object.prototype.toString.call(e);let a="";const y=n.length;a=y<10?"unknown:["+n+"]":n.slice(8,y-1),a=a.toLowerCase();let l=o.get(e);if(l===void 0)o.set(e,o.size);else return this.dispatch("[CIRCULAR:"+l+"]");if(typeof Buffer<"u"&&Buffer.isBuffer&&Buffer.isBuffer(e))return r("buffer:"),r(e.toString("utf8"));if(a!=="object"&&a!=="function"&&a!=="asyncfunction")this[a]?this[a](e):t.ignoreUnknown||this.unknown(e,a);else{let i=Object.keys(e);t.unorderedObjects&&(i=i.sort());let c=[];t.respectType!==!1&&!g(e)&&(c=b),t.excludeKeys&&(i=i.filter(s=>!t.excludeKeys(s)),c=c.filter(s=>!t.excludeKeys(s))),r("object:"+(i.length+c.length)+":");const d=s=>{this.dispatch(s),r(":"),t.excludeValues||this.dispatch(e[s]),r(",")};for(const s of i)d(s);for(const s of c)d(s)}},array(e,n){if(n=n??t.unorderedArrays!==!1,r("array:"+e.length+":"),!n||e.length<=1){for(const l of e)this.dispatch(l);return}const a=new Map,y=e.map(l=>{const i=p(t);i.dispatch(l);for(const[c,d]of i.getContext())a.set(c,d);return i.toString()});return o=a,y.sort(),this.array(y,!1)},date(e){return r("date:"+e.toJSON())},symbol(e){return r("symbol:"+e.toString())},unknown(e,n){if(r(n),!!e&&(r(":"),e&&typeof e.entries=="function"))return this.array([...e.entries()],!0)},error(e){return r("error:"+e.toString())},boolean(e){return r("bool:"+e)},string(e){r("string:"+e.length+":"),r(e)},function(e){r("fn:"),g(e)?this.dispatch("[native]"):this.dispatch(e.toString()),t.respectFunctionNames!==!1&&this.dispatch("function-name:"+String(e.name)),t.respectFunctionProperties&&this.object(e)},number(e){return r("number:"+e)},xml(e){return r("xml:"+e.toString())},null(){return r("Null")},undefined(){return r("Undefined")},regexp(e){return r("regex:"+e.toString())},uint8array(e){return r("uint8array:"),this.dispatch(Array.prototype.slice.call(e))},uint8clampedarray(e){return r("uint8clampedarray:"),this.dispatch(Array.prototype.slice.call(e))},int8array(e){return r("int8array:"),this.dispatch(Array.prototype.slice.call(e))},uint16array(e){return r("uint16array:"),this.dispatch(Array.prototype.slice.call(e))},int16array(e){return r("int16array:"),this.dispatch(Array.prototype.slice.call(e))},uint32array(e){return r("uint32array:"),this.dispatch(Array.prototype.slice.call(e))},int32array(e){return r("int32array:"),this.dispatch(Array.prototype.slice.call(e))},float32array(e){return r("float32array:"),this.dispatch(Array.prototype.slice.call(e))},float64array(e){return r("float64array:"),this.dispatch(Array.prototype.slice.call(e))},arraybuffer(e){return r("arraybuffer:"),this.dispatch(new Uint8Array(e))},url(e){return r("url:"+e.toString())},map(e){r("map:");const n=[...e];return this.array(n,t.unorderedSets!==!1)},set(e){r("set:");const n=[...e];return this.array(n,t.unorderedSets!==!1)},file(e){return r("file:"),this.dispatch([e.name,e.size,e.type,e.lastModfied])},blob(){if(t.ignoreUnknown)return r("[blob]");throw new Error(`Hashing Blob objects is currently not supported
1
+ const f=Object.freeze({ignoreUnknown:!1,respectType:!1,respectFunctionNames:!1,respectFunctionProperties:!1,unorderedObjects:!0,unorderedArrays:!1,unorderedSets:!1,excludeKeys:void 0,excludeValues:void 0,replacer:void 0});export function hashObject(t,o){o?o={...f,...o}:o=f;const u=p(o);return u.dispatch(t),u.toString()}const b=Object.freeze(["prototype","__proto__","constructor"]);function p(t){let o="",u=new Map;const r=e=>{o+=e};return{toString(){return o},getContext(){return u},dispatch(e){return t.replacer&&(e=t.replacer(e)),this[e===null?"null":typeof e](e)},object(e){if(e&&typeof e.toJSON=="function")return this.object(e.toJSON());const n=Object.prototype.toString.call(e);let a="";const y=n.length;y<10?a="unknown:["+n+"]":a=n.slice(8,y-1),a=a.toLowerCase();let l=null;if((l=u.get(e))===void 0)u.set(e,u.size);else return this.dispatch("[CIRCULAR:"+l+"]");if(typeof Buffer<"u"&&Buffer.isBuffer&&Buffer.isBuffer(e))return r("buffer:"),r(e.toString("utf8"));if(a!=="object"&&a!=="function"&&a!=="asyncfunction")this[a]?this[a](e):t.ignoreUnknown||this.unknown(e,a);else{let i=Object.keys(e);t.unorderedObjects&&(i=i.sort());let c=[];t.respectType!==!1&&!g(e)&&(c=b),t.excludeKeys&&(i=i.filter(s=>!t.excludeKeys(s)),c=c.filter(s=>!t.excludeKeys(s))),r("object:"+(i.length+c.length)+":");const d=s=>{this.dispatch(s),r(":"),t.excludeValues||this.dispatch(e[s]),r(",")};for(const s of i)d(s);for(const s of c)d(s)}},array(e,n){if(n=n===void 0?t.unorderedArrays!==!1:n,r("array:"+e.length+":"),!n||e.length<=1){for(const l of e)this.dispatch(l);return}const a=new Map,y=e.map(l=>{const i=p(t);i.dispatch(l);for(const[c,d]of i.getContext())a.set(c,d);return i.toString()});return u=a,y.sort(),this.array(y,!1)},date(e){return r("date:"+e.toJSON())},symbol(e){return r("symbol:"+e.toString())},unknown(e,n){if(r(n),!!e&&(r(":"),e&&typeof e.entries=="function"))return this.array(Array.from(e.entries()),!0)},error(e){return r("error:"+e.toString())},boolean(e){return r("bool:"+e)},string(e){r("string:"+e.length+":"),r(e)},function(e){r("fn:"),g(e)?this.dispatch("[native]"):this.dispatch(e.toString()),t.respectFunctionNames!==!1&&this.dispatch("function-name:"+String(e.name)),t.respectFunctionProperties&&this.object(e)},number(e){return r("number:"+e)},xml(e){return r("xml:"+e.toString())},null(){return r("Null")},undefined(){return r("Undefined")},regexp(e){return r("regex:"+e.toString())},uint8array(e){return r("uint8array:"),this.dispatch(Array.prototype.slice.call(e))},uint8clampedarray(e){return r("uint8clampedarray:"),this.dispatch(Array.prototype.slice.call(e))},int8array(e){return r("int8array:"),this.dispatch(Array.prototype.slice.call(e))},uint16array(e){return r("uint16array:"),this.dispatch(Array.prototype.slice.call(e))},int16array(e){return r("int16array:"),this.dispatch(Array.prototype.slice.call(e))},uint32array(e){return r("uint32array:"),this.dispatch(Array.prototype.slice.call(e))},int32array(e){return r("int32array:"),this.dispatch(Array.prototype.slice.call(e))},float32array(e){return r("float32array:"),this.dispatch(Array.prototype.slice.call(e))},float64array(e){return r("float64array:"),this.dispatch(Array.prototype.slice.call(e))},arraybuffer(e){return r("arraybuffer:"),this.dispatch(new Uint8Array(e))},url(e){return r("url:"+e.toString())},map(e){r("map:");const n=[...e];return this.array(n,t.unorderedSets!==!1)},set(e){r("set:");const n=[...e];return this.array(n,t.unorderedSets!==!1)},file(e){return r("file:"),this.dispatch([e.name,e.size,e.type,e.lastModfied])},blob(){if(t.ignoreUnknown)return r("[blob]");throw new Error(`Hashing Blob objects is currently not supported
2
2
  Use "options.replacer" or "options.ignoreUnknown"
3
3
  `)},domwindow(){return r("domwindow")},bigint(e){return r("bigint:"+e.toString())},process(){return r("process")},timer(){return r("timer")},pipe(){return r("pipe")},tcp(){return r("tcp")},udp(){return r("udp")},tty(){return r("tty")},statwatcher(){return r("statwatcher")},securecontext(){return r("securecontext")},connection(){return r("connection")},zlib(){return r("zlib")},context(){return r("context")},nodescript(){return r("nodescript")},httpparser(){return r("httpparser")},dataview(){return r("dataview")},signal(){return r("signal")},fsevent(){return r("fsevent")},tlswrap(){return r("tlswrap")}}}const h="[native code] }",w=h.length;function g(t){return typeof t!="function"?!1:Function.prototype.toString.call(t).slice(-w)===h}
package/dist/sha-256.cjs CHANGED
@@ -3,57 +3,152 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.SHA256 = void 0;
6
+ exports.WordArray = exports.Utf8 = exports.SHA256 = exports.Latin1 = exports.Hex = exports.Hasher = exports.BufferedBlockAlgorithm = exports.Base64 = void 0;
7
7
  exports.sha256 = sha256;
8
8
  exports.sha256base64 = sha256base64;
9
- var _hasher = require("./hasher.cjs");
10
- const x = [1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225],
11
- S = [1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998],
12
- r = [];
13
- class SHA256 extends _hasher.Hasher {
14
- _hash = new _hasher.WordArray([...x]);
9
+ class WordArray {
10
+ words;
11
+ sigBytes;
12
+ constructor(_, s) {
13
+ _ = this.words = _ || [], this.sigBytes = s === void 0 ? _.length * 4 : s;
14
+ }
15
+ toString(_) {
16
+ return (_ || Hex).stringify(this);
17
+ }
18
+ concat(_) {
19
+ if (this.clamp(), this.sigBytes % 4) for (let s = 0; s < _.sigBytes; s++) {
20
+ const t = _.words[s >>> 2] >>> 24 - s % 4 * 8 & 255;
21
+ this.words[this.sigBytes + s >>> 2] |= t << 24 - (this.sigBytes + s) % 4 * 8;
22
+ } else for (let s = 0; s < _.sigBytes; s += 4) this.words[this.sigBytes + s >>> 2] = _.words[s >>> 2];
23
+ return this.sigBytes += _.sigBytes, this;
24
+ }
25
+ clamp() {
26
+ this.words[this.sigBytes >>> 2] &= 4294967295 << 32 - this.sigBytes % 4 * 8, this.words.length = Math.ceil(this.sigBytes / 4);
27
+ }
28
+ clone() {
29
+ return new WordArray([...this.words]);
30
+ }
31
+ }
32
+ exports.WordArray = WordArray;
33
+ const Hex = exports.Hex = {
34
+ stringify(e) {
35
+ const _ = [];
36
+ for (let s = 0; s < e.sigBytes; s++) {
37
+ const t = e.words[s >>> 2] >>> 24 - s % 4 * 8 & 255;
38
+ _.push((t >>> 4).toString(16), (t & 15).toString(16));
39
+ }
40
+ return _.join("");
41
+ }
42
+ },
43
+ Base64 = exports.Base64 = {
44
+ stringify(e) {
45
+ const _ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
46
+ s = [];
47
+ for (let t = 0; t < e.sigBytes; t += 3) {
48
+ const i = e.words[t >>> 2] >>> 24 - t % 4 * 8 & 255,
49
+ a = e.words[t + 1 >>> 2] >>> 24 - (t + 1) % 4 * 8 & 255,
50
+ r = e.words[t + 2 >>> 2] >>> 24 - (t + 2) % 4 * 8 & 255,
51
+ c = i << 16 | a << 8 | r;
52
+ for (let n = 0; n < 4 && t * 8 + n * 6 < e.sigBytes * 8; n++) s.push(_.charAt(c >>> 6 * (3 - n) & 63));
53
+ }
54
+ return s.join("");
55
+ }
56
+ },
57
+ Latin1 = exports.Latin1 = {
58
+ parse(e) {
59
+ const _ = e.length,
60
+ s = [];
61
+ for (let t = 0; t < _; t++) s[t >>> 2] |= (e.charCodeAt(t) & 255) << 24 - t % 4 * 8;
62
+ return new WordArray(s, _);
63
+ }
64
+ },
65
+ Utf8 = exports.Utf8 = {
66
+ parse(e) {
67
+ return Latin1.parse(unescape(encodeURIComponent(e)));
68
+ }
69
+ };
70
+ class BufferedBlockAlgorithm {
71
+ _data = new WordArray();
72
+ _nDataBytes = 0;
73
+ _minBufferSize = 0;
74
+ blockSize = 512 / 32;
15
75
  reset() {
16
- super.reset(), this._hash = new _hasher.WordArray([...x]);
17
- }
18
- _doProcessBlock(m, c) {
19
- const _ = this._hash.words;
20
- let s = _[0],
21
- a = _[1],
22
- n = _[2],
23
- g = _[3],
24
- e = _[4],
25
- d = _[5],
26
- l = _[6],
27
- u = _[7];
28
- for (let t = 0; t < 64; t++) {
29
- if (t < 16) r[t] = Math.trunc(m[c + t]);else {
30
- const o = r[t - 15],
31
- H = (o << 25 | o >>> 7) ^ (o << 14 | o >>> 18) ^ o >>> 3,
32
- i = r[t - 2],
33
- M = (i << 15 | i >>> 17) ^ (i << 13 | i >>> 19) ^ i >>> 10;
34
- r[t] = H + r[t - 7] + M + r[t - 16];
76
+ this._data = new WordArray(), this._nDataBytes = 0;
77
+ }
78
+ _append(_) {
79
+ typeof _ == "string" && (_ = Utf8.parse(_)), this._data.concat(_), this._nDataBytes += _.sigBytes;
80
+ }
81
+ _doProcessBlock(_, s) {}
82
+ _process(_) {
83
+ let s,
84
+ t = this._data.sigBytes / (this.blockSize * 4);
85
+ _ ? t = Math.ceil(t) : t = Math.max((t | 0) - this._minBufferSize, 0);
86
+ const i = t * this.blockSize,
87
+ a = Math.min(i * 4, this._data.sigBytes);
88
+ if (i) {
89
+ for (let r = 0; r < i; r += this.blockSize) this._doProcessBlock(this._data.words, r);
90
+ s = this._data.words.splice(0, i), this._data.sigBytes -= a;
91
+ }
92
+ return new WordArray(s, a);
93
+ }
94
+ }
95
+ exports.BufferedBlockAlgorithm = BufferedBlockAlgorithm;
96
+ class Hasher extends BufferedBlockAlgorithm {
97
+ update(_) {
98
+ return this._append(_), this._process(), this;
99
+ }
100
+ finalize(_) {
101
+ _ && this._append(_);
102
+ }
103
+ }
104
+ exports.Hasher = Hasher;
105
+ const p = [1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225],
106
+ k = [1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998],
107
+ h = [];
108
+ class SHA256 extends Hasher {
109
+ _hash = new WordArray([...p]);
110
+ reset() {
111
+ super.reset(), this._hash = new WordArray([...p]);
112
+ }
113
+ _doProcessBlock(_, s) {
114
+ const t = this._hash.words;
115
+ let i = t[0],
116
+ a = t[1],
117
+ r = t[2],
118
+ c = t[3],
119
+ n = t[4],
120
+ g = t[5],
121
+ d = t[6],
122
+ B = t[7];
123
+ for (let o = 0; o < 64; o++) {
124
+ if (o < 16) h[o] = _[s + o] | 0;else {
125
+ const f = h[o - 15],
126
+ S = (f << 25 | f >>> 7) ^ (f << 14 | f >>> 18) ^ f >>> 3,
127
+ l = h[o - 2],
128
+ z = (l << 15 | l >>> 17) ^ (l << 13 | l >>> 19) ^ l >>> 10;
129
+ h[o] = S + h[o - 7] + z + h[o - 16];
35
130
  }
36
- const p = e & d ^ ~e & l,
37
- B = s & a ^ s & n ^ a & n,
38
- b = (s << 30 | s >>> 2) ^ (s << 19 | s >>> 13) ^ (s << 10 | s >>> 22),
39
- y = (e << 26 | e >>> 6) ^ (e << 21 | e >>> 11) ^ (e << 7 | e >>> 25),
40
- f = u + y + p + S[t] + r[t],
41
- z = b + B;
42
- u = l, l = d, d = e, e = Math.trunc(g + f), g = n, n = a, a = s, s = Math.trunc(f + z);
131
+ const u = n & g ^ ~n & d,
132
+ x = i & a ^ i & r ^ a & r,
133
+ m = (i << 30 | i >>> 2) ^ (i << 19 | i >>> 13) ^ (i << 10 | i >>> 22),
134
+ b = (n << 26 | n >>> 6) ^ (n << 21 | n >>> 11) ^ (n << 7 | n >>> 25),
135
+ y = B + b + u + k[o] + h[o],
136
+ w = m + x;
137
+ B = d, d = g, g = n, n = c + y | 0, c = r, r = a, a = i, i = y + w | 0;
43
138
  }
44
- _[0] = _[0] + s | 0, _[1] = _[1] + a | 0, _[2] = _[2] + n | 0, _[3] = _[3] + g | 0, _[4] = _[4] + e | 0, _[5] = _[5] + d | 0, _[6] = _[6] + l | 0, _[7] = _[7] + u | 0;
139
+ t[0] = t[0] + i | 0, t[1] = t[1] + a | 0, t[2] = t[2] + r | 0, t[3] = t[3] + c | 0, t[4] = t[4] + n | 0, t[5] = t[5] + g | 0, t[6] = t[6] + d | 0, t[7] = t[7] + B | 0;
45
140
  }
46
- finalize(m) {
47
- super.finalize(m);
48
- const c = this._nDataBytes * 8,
49
- _ = this._data.sigBytes * 8;
50
- return this._data.words[_ >>> 5] |= 128 << 24 - _ % 32, this._data.words[(_ + 64 >>> 9 << 4) + 14] = Math.floor(c / 4294967296), this._data.words[(_ + 64 >>> 9 << 4) + 15] = c, this._data.sigBytes = this._data.words.length * 4, this._process(), this._hash;
141
+ finalize(_) {
142
+ super.finalize(_);
143
+ const s = this._nDataBytes * 8,
144
+ t = this._data.sigBytes * 8;
145
+ return this._data.words[t >>> 5] |= 128 << 24 - t % 32, this._data.words[(t + 64 >>> 9 << 4) + 14] = Math.floor(s / 4294967296), this._data.words[(t + 64 >>> 9 << 4) + 15] = s, this._data.sigBytes = this._data.words.length * 4, this._process(), this._hash;
51
146
  }
52
147
  }
53
148
  exports.SHA256 = SHA256;
54
- function sha256(h) {
55
- return new SHA256().finalize(h).toString();
149
+ function sha256(e) {
150
+ return new SHA256().finalize(e).toString();
56
151
  }
57
- function sha256base64(h) {
58
- return new SHA256().finalize(h).toString(_hasher.Base64);
152
+ function sha256base64(e) {
153
+ return new SHA256().finalize(e).toString(Base64);
59
154
  }
package/dist/sha-256.d.ts CHANGED
@@ -1,4 +1,38 @@
1
- import { Hasher, WordArray } from "./hasher";
1
+ export declare class WordArray {
2
+ words: number[];
3
+ sigBytes: number;
4
+ constructor(words?: number[], sigBytes?: number);
5
+ toString(encoder?: typeof Hex): string;
6
+ concat(wordArray: WordArray): this;
7
+ clamp(): void;
8
+ clone(): WordArray;
9
+ }
10
+ export declare const Hex: {
11
+ stringify(wordArray: WordArray): string;
12
+ };
13
+ export declare const Base64: {
14
+ stringify(wordArray: WordArray): string;
15
+ };
16
+ export declare const Latin1: {
17
+ parse(latin1Str: string): WordArray;
18
+ };
19
+ export declare const Utf8: {
20
+ parse(utf8Str: string): WordArray;
21
+ };
22
+ export declare class BufferedBlockAlgorithm {
23
+ _data: WordArray;
24
+ _nDataBytes: number;
25
+ _minBufferSize: number;
26
+ blockSize: number;
27
+ reset(): void;
28
+ _append(data: string | WordArray): void;
29
+ _doProcessBlock(_dataWords: any, _offset: any): void;
30
+ _process(doFlush?: boolean): WordArray;
31
+ }
32
+ export declare class Hasher extends BufferedBlockAlgorithm {
33
+ update(messageUpdate: string): this;
34
+ finalize(messageUpdate: string): void;
35
+ }
2
36
  /**
3
37
  * SHA-256 hash algorithm.
4
38
  */
@@ -12,22 +46,22 @@ export declare class SHA256 extends Hasher {
12
46
  /**
13
47
  * Finishes the hash calculation and returns the hash as a WordArray.
14
48
  *
15
- * @param messageUpdate - Additional message content to include in the hash.
16
- * @returns The finalized hash as a WordArray.
49
+ * @param {string} messageUpdate - Additional message content to include in the hash.
50
+ * @returns {WordArray} The finalised hash as a WordArray.
17
51
  */
18
52
  finalize(messageUpdate: string): WordArray;
19
53
  }
20
54
  /**
21
55
  * Calculates the SHA-256 hash of the message provided.
22
56
  *
23
- * @param message - The message to hash.
24
- * @returns The message hash as a hexadecimal string.
57
+ * @param {string} message - The message to hash.
58
+ * @returns {string} The message hash as a hexadecimal string.
25
59
  */
26
60
  export declare function sha256(message: string): string;
27
61
  /**
28
62
  * Calculates the SHA-256 hash of the given message and encodes it in Base64.
29
63
  *
30
- * @param message - The message to hash.
31
- * @returns The base64 encoded hash of the message.
64
+ * @param {string} message - The message to hash.
65
+ * @returns {string} The base64 encoded hash of the message.
32
66
  */
33
67
  export declare function sha256base64(message: string): string;
package/dist/sha-256.mjs CHANGED
@@ -1 +1 @@
1
- import{Base64 as v,Hasher as A,WordArray as w}from"./hasher";const x=[1779033703,-1150833019,1013904242,-1521486534,1359893119,-1694144372,528734635,1541459225],S=[1116352408,1899447441,-1245643825,-373957723,961987163,1508970993,-1841331548,-1424204075,-670586216,310598401,607225278,1426881987,1925078388,-2132889090,-1680079193,-1046744716,-459576895,-272742522,264347078,604807628,770255983,1249150122,1555081692,1996064986,-1740746414,-1473132947,-1341970488,-1084653625,-958395405,-710438585,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,-2117940946,-1838011259,-1564481375,-1474664885,-1035236496,-949202525,-778901479,-694614492,-200395387,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,-2067236844,-1933114872,-1866530822,-1538233109,-1090935817,-965641998],r=[];export class SHA256 extends A{_hash=new w([...x]);reset(){super.reset(),this._hash=new w([...x])}_doProcessBlock(m,c){const _=this._hash.words;let s=_[0],a=_[1],n=_[2],g=_[3],e=_[4],d=_[5],l=_[6],u=_[7];for(let t=0;t<64;t++){if(t<16)r[t]=Math.trunc(m[c+t]);else{const o=r[t-15],H=(o<<25|o>>>7)^(o<<14|o>>>18)^o>>>3,i=r[t-2],M=(i<<15|i>>>17)^(i<<13|i>>>19)^i>>>10;r[t]=H+r[t-7]+M+r[t-16]}const p=e&d^~e&l,B=s&a^s&n^a&n,b=(s<<30|s>>>2)^(s<<19|s>>>13)^(s<<10|s>>>22),y=(e<<26|e>>>6)^(e<<21|e>>>11)^(e<<7|e>>>25),f=u+y+p+S[t]+r[t],z=b+B;u=l,l=d,d=e,e=Math.trunc(g+f),g=n,n=a,a=s,s=Math.trunc(f+z)}_[0]=_[0]+s|0,_[1]=_[1]+a|0,_[2]=_[2]+n|0,_[3]=_[3]+g|0,_[4]=_[4]+e|0,_[5]=_[5]+d|0,_[6]=_[6]+l|0,_[7]=_[7]+u|0}finalize(m){super.finalize(m);const c=this._nDataBytes*8,_=this._data.sigBytes*8;return this._data.words[_>>>5]|=128<<24-_%32,this._data.words[(_+64>>>9<<4)+14]=Math.floor(c/4294967296),this._data.words[(_+64>>>9<<4)+15]=c,this._data.sigBytes=this._data.words.length*4,this._process(),this._hash}}export function sha256(h){return new SHA256().finalize(h).toString()}export function sha256base64(h){return new SHA256().finalize(h).toString(v)}
1
+ export class WordArray{words;sigBytes;constructor(_,s){_=this.words=_||[],this.sigBytes=s===void 0?_.length*4:s}toString(_){return(_||Hex).stringify(this)}concat(_){if(this.clamp(),this.sigBytes%4)for(let s=0;s<_.sigBytes;s++){const t=_.words[s>>>2]>>>24-s%4*8&255;this.words[this.sigBytes+s>>>2]|=t<<24-(this.sigBytes+s)%4*8}else for(let s=0;s<_.sigBytes;s+=4)this.words[this.sigBytes+s>>>2]=_.words[s>>>2];return this.sigBytes+=_.sigBytes,this}clamp(){this.words[this.sigBytes>>>2]&=4294967295<<32-this.sigBytes%4*8,this.words.length=Math.ceil(this.sigBytes/4)}clone(){return new WordArray([...this.words])}}export const Hex={stringify(e){const _=[];for(let s=0;s<e.sigBytes;s++){const t=e.words[s>>>2]>>>24-s%4*8&255;_.push((t>>>4).toString(16),(t&15).toString(16))}return _.join("")}},Base64={stringify(e){const _="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",s=[];for(let t=0;t<e.sigBytes;t+=3){const i=e.words[t>>>2]>>>24-t%4*8&255,a=e.words[t+1>>>2]>>>24-(t+1)%4*8&255,r=e.words[t+2>>>2]>>>24-(t+2)%4*8&255,c=i<<16|a<<8|r;for(let n=0;n<4&&t*8+n*6<e.sigBytes*8;n++)s.push(_.charAt(c>>>6*(3-n)&63))}return s.join("")}},Latin1={parse(e){const _=e.length,s=[];for(let t=0;t<_;t++)s[t>>>2]|=(e.charCodeAt(t)&255)<<24-t%4*8;return new WordArray(s,_)}},Utf8={parse(e){return Latin1.parse(unescape(encodeURIComponent(e)))}};export class BufferedBlockAlgorithm{_data=new WordArray;_nDataBytes=0;_minBufferSize=0;blockSize=512/32;reset(){this._data=new WordArray,this._nDataBytes=0}_append(_){typeof _=="string"&&(_=Utf8.parse(_)),this._data.concat(_),this._nDataBytes+=_.sigBytes}_doProcessBlock(_,s){}_process(_){let s,t=this._data.sigBytes/(this.blockSize*4);_?t=Math.ceil(t):t=Math.max((t|0)-this._minBufferSize,0);const i=t*this.blockSize,a=Math.min(i*4,this._data.sigBytes);if(i){for(let r=0;r<i;r+=this.blockSize)this._doProcessBlock(this._data.words,r);s=this._data.words.splice(0,i),this._data.sigBytes-=a}return new WordArray(s,a)}}export class Hasher extends BufferedBlockAlgorithm{update(_){return this._append(_),this._process(),this}finalize(_){_&&this._append(_)}}const p=[1779033703,-1150833019,1013904242,-1521486534,1359893119,-1694144372,528734635,1541459225],k=[1116352408,1899447441,-1245643825,-373957723,961987163,1508970993,-1841331548,-1424204075,-670586216,310598401,607225278,1426881987,1925078388,-2132889090,-1680079193,-1046744716,-459576895,-272742522,264347078,604807628,770255983,1249150122,1555081692,1996064986,-1740746414,-1473132947,-1341970488,-1084653625,-958395405,-710438585,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,-2117940946,-1838011259,-1564481375,-1474664885,-1035236496,-949202525,-778901479,-694614492,-200395387,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,-2067236844,-1933114872,-1866530822,-1538233109,-1090935817,-965641998],h=[];export class SHA256 extends Hasher{_hash=new WordArray([...p]);reset(){super.reset(),this._hash=new WordArray([...p])}_doProcessBlock(_,s){const t=this._hash.words;let i=t[0],a=t[1],r=t[2],c=t[3],n=t[4],g=t[5],d=t[6],B=t[7];for(let o=0;o<64;o++){if(o<16)h[o]=_[s+o]|0;else{const f=h[o-15],S=(f<<25|f>>>7)^(f<<14|f>>>18)^f>>>3,l=h[o-2],z=(l<<15|l>>>17)^(l<<13|l>>>19)^l>>>10;h[o]=S+h[o-7]+z+h[o-16]}const u=n&g^~n&d,x=i&a^i&r^a&r,m=(i<<30|i>>>2)^(i<<19|i>>>13)^(i<<10|i>>>22),b=(n<<26|n>>>6)^(n<<21|n>>>11)^(n<<7|n>>>25),y=B+b+u+k[o]+h[o],w=m+x;B=d,d=g,g=n,n=c+y|0,c=r,r=a,a=i,i=y+w|0}t[0]=t[0]+i|0,t[1]=t[1]+a|0,t[2]=t[2]+r|0,t[3]=t[3]+c|0,t[4]=t[4]+n|0,t[5]=t[5]+g|0,t[6]=t[6]+d|0,t[7]=t[7]+B|0}finalize(_){super.finalize(_);const s=this._nDataBytes*8,t=this._data.sigBytes*8;return this._data.words[t>>>5]|=128<<24-t%32,this._data.words[(t+64>>>9<<4)+14]=Math.floor(s/4294967296),this._data.words[(t+64>>>9<<4)+15]=s,this._data.sigBytes=this._data.words.length*4,this._process(),this._hash}}export function sha256(e){return new SHA256().finalize(e).toString()}export function sha256base64(e){return new SHA256().finalize(e).toString(Base64)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/hash",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "description": "A package containing utility functions that hash data using various algorithms.",
6
6
  "repository": {
@@ -9,7 +9,7 @@
9
9
  "directory": "packages/hash"
10
10
  },
11
11
  "private": false,
12
- "dependencies": { "js-xxhash": "^4.0.0", "@stryke/types": ">=0.1.1" },
12
+ "dependencies": { "js-xxhash": "^4.0.0", "@stryke/types": ">=0.1.2" },
13
13
  "publishConfig": { "access": "public" },
14
14
  "devDependencies": {},
15
15
  "sideEffects": false,