kestrel.markets
Version:
A typed, token-efficient language + runtime for agentic trading: agents author bounded plans, the runtime fires them at the tick. CLI + typed library + MCP server.
154 lines (152 loc) • 3.49 kB
JavaScript
// src/crypto/sha256.ts
var BUN_CRYPTO_HASHER = globalThis.Bun?.CryptoHasher;
var K = new Uint32Array([
1116352408,
1899447441,
3049323471,
3921009573,
961987163,
1508970993,
2453635748,
2870763221,
3624381080,
310598401,
607225278,
1426881987,
1925078388,
2162078206,
2614888103,
3248222580,
3835390401,
4022224774,
264347078,
604807628,
770255983,
1249150122,
1555081692,
1996064986,
2554220882,
2821834349,
2952996808,
3210313671,
3336571891,
3584528711,
113926993,
338241895,
666307205,
773529912,
1294757372,
1396182291,
1695183700,
1986661051,
2177026350,
2456956037,
2730485921,
2820302411,
3259730800,
3345764771,
3516065817,
3600352804,
4094571909,
275423344,
430227734,
506948616,
659060556,
883997877,
958139571,
1322822218,
1537002063,
1747873779,
1955562222,
2024104815,
2227730452,
2361852424,
2428436474,
2756734187,
3204031479,
3329325298
]);
function rotr(x, n) {
return x >>> n | x << 32 - n;
}
function sha256Pure(text) {
const msg = new TextEncoder().encode(text);
const bitLen = msg.length * 8;
const withPad = new Uint8Array((msg.length + 8 >> 6) * 64 + 64);
withPad.set(msg);
withPad[msg.length] = 128;
const hi = Math.floor(bitLen / 4294967296);
const lo = bitLen >>> 0;
const lenOff = withPad.length - 8;
withPad[lenOff] = hi >>> 24 & 255;
withPad[lenOff + 1] = hi >>> 16 & 255;
withPad[lenOff + 2] = hi >>> 8 & 255;
withPad[lenOff + 3] = hi & 255;
withPad[lenOff + 4] = lo >>> 24 & 255;
withPad[lenOff + 5] = lo >>> 16 & 255;
withPad[lenOff + 6] = lo >>> 8 & 255;
withPad[lenOff + 7] = lo & 255;
let h0 = 1779033703;
let h1 = 3144134277;
let h2 = 1013904242;
let h3 = 2773480762;
let h4 = 1359893119;
let h5 = 2600822924;
let h6 = 528734635;
let h7 = 1541459225;
const w = new Uint32Array(64);
for (let off = 0;off < withPad.length; off += 64) {
for (let i = 0;i < 16; i++) {
const j = off + i * 4;
w[i] = (withPad[j] << 24 | withPad[j + 1] << 16 | withPad[j + 2] << 8 | withPad[j + 3]) >>> 0;
}
for (let i = 16;i < 64; i++) {
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ w[i - 2] >>> 10;
w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
}
let a = h0;
let b = h1;
let c = h2;
let d = h3;
let e = h4;
let f = h5;
let g = h6;
let h = h7;
for (let i = 0;i < 64; i++) {
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
const ch = e & f ^ ~e & g;
const t1 = h + S1 + ch + K[i] + w[i] >>> 0;
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
const maj = a & b ^ a & c ^ b & c;
const t2 = S0 + maj >>> 0;
h = g;
g = f;
f = e;
e = d + t1 >>> 0;
d = c;
c = b;
b = a;
a = t1 + t2 >>> 0;
}
h0 = h0 + a >>> 0;
h1 = h1 + b >>> 0;
h2 = h2 + c >>> 0;
h3 = h3 + d >>> 0;
h4 = h4 + e >>> 0;
h5 = h5 + f >>> 0;
h6 = h6 + g >>> 0;
h7 = h7 + h >>> 0;
}
return hex32(h0) + hex32(h1) + hex32(h2) + hex32(h3) + hex32(h4) + hex32(h5) + hex32(h6) + hex32(h7);
}
function hex32(x) {
return (x >>> 0).toString(16).padStart(8, "0");
}
function sha256(text) {
if (BUN_CRYPTO_HASHER !== undefined) {
return new BUN_CRYPTO_HASHER("sha256").update(text).digest("hex");
}
return sha256Pure(text);
}
export { sha256 };