The main page and direct photo links open without a prompt. Each photo's pool is sealed with its own key, handed out by POST /k (served by the daemon in serve and export mode). The hold-and-solve card appears only for low-entropy clicks into a collection, too many low-entropy landings from one IP, or more than photo_limit photos per window, with escalating proof-of-work and a 429 cap. Build ids now sort chronologically within the same second. Co-Authored-By: Claude Opus 5.5 <[email protected]>
199 lines
5.9 KiB
TypeScript
199 lines
5.9 KiB
TypeScript
const K = new Int32Array([
|
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be,
|
|
0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa,
|
|
0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85,
|
|
0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
|
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
|
|
0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
]);
|
|
|
|
const IV = new Int32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
|
|
|
|
function expand(w: Int32Array, from: number): void {
|
|
for (let i = from; i < 64; i++) {
|
|
const x = w[i - 15];
|
|
const y = w[i - 2];
|
|
const s0 = ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
|
|
const s1 = ((y >>> 17) | (y << 15)) ^ ((y >>> 19) | (y << 13)) ^ (y >>> 10);
|
|
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
|
|
}
|
|
}
|
|
|
|
function rounds(s: Int32Array, w: Int32Array, from: number, to: number): void {
|
|
let a = s[0];
|
|
let b = s[1];
|
|
let c = s[2];
|
|
let d = s[3];
|
|
let e = s[4];
|
|
let f = s[5];
|
|
let g = s[6];
|
|
let h = s[7];
|
|
for (let i = from; i < to; i++) {
|
|
const t1 =
|
|
(h + (((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7))) + ((e & f) ^ (~e & g)) + K[i] + w[i]) | 0;
|
|
const t2 = ((((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10))) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
|
|
h = g;
|
|
g = f;
|
|
f = e;
|
|
e = (d + t1) | 0;
|
|
d = c;
|
|
c = b;
|
|
b = a;
|
|
a = (t1 + t2) | 0;
|
|
}
|
|
s[0] = a;
|
|
s[1] = b;
|
|
s[2] = c;
|
|
s[3] = d;
|
|
s[4] = e;
|
|
s[5] = f;
|
|
s[6] = g;
|
|
s[7] = h;
|
|
}
|
|
|
|
const W = new Int32Array(64);
|
|
const S = new Int32Array(8);
|
|
const H = new Int32Array(8);
|
|
|
|
export function sha256(message: Uint8Array, out: Uint8Array = new Uint8Array(32)): Uint8Array {
|
|
H.set(IV);
|
|
const length = message.length;
|
|
const blocks = Math.ceil((length + 9) / 64);
|
|
for (let block = 0; block < blocks; block++) {
|
|
const base = block * 64;
|
|
for (let i = 0; i < 16; i++) {
|
|
let word = 0;
|
|
for (let j = 0; j < 4; j++) {
|
|
const at = base + i * 4 + j;
|
|
let byte = 0;
|
|
if (at < length) {
|
|
byte = message[at];
|
|
} else if (at === length) {
|
|
byte = 0x80;
|
|
} else if (block === blocks - 1 && at >= base + 56) {
|
|
const bits = at - base - 56;
|
|
byte = bits < 3 ? 0 : bits === 3 ? (length >>> 29) & 255 : ((length * 8) >>> ((7 - bits) * 8)) & 255;
|
|
}
|
|
word = (word << 8) | byte;
|
|
}
|
|
W[i] = word;
|
|
}
|
|
expand(W, 16);
|
|
S.set(H);
|
|
rounds(S, W, 0, 64);
|
|
for (let i = 0; i < 8; i++) {
|
|
H[i] = (H[i] + S[i]) | 0;
|
|
}
|
|
}
|
|
for (let i = 0; i < 8; i++) {
|
|
out[i * 4] = H[i] >>> 24;
|
|
out[i * 4 + 1] = (H[i] >>> 16) & 255;
|
|
out[i * 4 + 2] = (H[i] >>> 8) & 255;
|
|
out[i * 4 + 3] = H[i] & 255;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export const nonceSpan = 4294967296;
|
|
|
|
export class PowSearch {
|
|
private readonly w = new Int32Array(64);
|
|
private readonly mid = new Int32Array(8);
|
|
private readonly s = new Int32Array(8);
|
|
private readonly shift: number;
|
|
|
|
constructor(token: string, bits: number) {
|
|
const w = this.w;
|
|
for (let i = 0; i < 8; i++) {
|
|
w[i] =
|
|
(token.charCodeAt(i * 4) << 24) |
|
|
(token.charCodeAt(i * 4 + 1) << 16) |
|
|
(token.charCodeAt(i * 4 + 2) << 8) |
|
|
token.charCodeAt(i * 4 + 3);
|
|
}
|
|
w[10] = 0x80000000 | 0;
|
|
w[15] = 320;
|
|
this.mid.set(IV);
|
|
rounds(this.mid, w, 0, 8);
|
|
this.shift = 32 - Math.max(1, Math.min(32, bits));
|
|
}
|
|
|
|
run(hi: number, from: number, to: number): number {
|
|
const w = this.w;
|
|
const s = this.s;
|
|
const mid = this.mid;
|
|
const shift = this.shift;
|
|
const h0 = IV[0];
|
|
w[8] = hi | 0;
|
|
for (let lo = from; lo < to; lo++) {
|
|
w[9] = lo | 0;
|
|
expand(w, 16);
|
|
s.set(mid);
|
|
rounds(s, w, 8, 64);
|
|
if ((h0 + s[0]) >>> shift === 0) {
|
|
return lo;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
export const sealMagic = "luphoto-sealed-1";
|
|
const nonceSize = 12;
|
|
|
|
export function fromHex(text: string): Uint8Array {
|
|
const out = new Uint8Array(text.length >>> 1);
|
|
for (let i = 0; i < out.length; i++) {
|
|
out[i] = parseInt(text.substr(i * 2, 2), 16);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function toHex(bytes: Uint8Array): string {
|
|
let text = "";
|
|
for (let i = 0; i < bytes.length; i++) {
|
|
text += (bytes[i] < 16 ? "0" : "") + bytes[i].toString(16);
|
|
}
|
|
return text;
|
|
}
|
|
|
|
export function fromBase64(text: string): Uint8Array {
|
|
const raw = atob(text);
|
|
const out = new Uint8Array(raw.length);
|
|
for (let i = 0; i < raw.length; i++) {
|
|
out[i] = raw.charCodeAt(i);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function unseal(key: Uint8Array, sealed: string): Uint8Array | null {
|
|
const data = fromBase64(sealed);
|
|
if (data.length < nonceSize + sealMagic.length) {
|
|
return null;
|
|
}
|
|
const block = new Uint8Array(32 + nonceSize + 4);
|
|
block.set(key.subarray(0, 32));
|
|
block.set(data.subarray(0, nonceSize), 32);
|
|
const stream = new Uint8Array(32);
|
|
const body = data.subarray(nonceSize);
|
|
for (let i = 0, at = 0; at < body.length; i++, at += 32) {
|
|
block[44] = i >>> 24;
|
|
block[45] = (i >>> 16) & 255;
|
|
block[46] = (i >>> 8) & 255;
|
|
block[47] = i & 255;
|
|
sha256(block, stream);
|
|
const end = Math.min(32, body.length - at);
|
|
for (let j = 0; j < end; j++) {
|
|
body[at + j] ^= stream[j];
|
|
}
|
|
if (i === 0) {
|
|
for (let j = 0; j < sealMagic.length; j++) {
|
|
if (body[j] !== sealMagic.charCodeAt(j)) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return body.subarray(sealMagic.length);
|
|
}
|