Skip to content

fix(crypto): type no-arg digest() result as Buffer, not string (#1353)#1394

Merged
proggeramlug merged 1 commit into
mainfrom
feat/crypto-digest-buffer-1353
May 22, 2026
Merged

fix(crypto): type no-arg digest() result as Buffer, not string (#1353)#1394
proggeramlug merged 1 commit into
mainfrom
feat/crypto-digest-buffer-1353

Conversation

@proggeramlug
Copy link
Copy Markdown
Contributor

createHash(alg).update(d).digest() with no encoding arg returns a Buffer, but the codegen typed every digest chain as String. So:

  • const buf = hash.digest(); buf.toString('hex') mis-read the buffer bytes as a Latin-1 string (returned garbage instead of hex).
  • buf[0] returned a character instead of the numeric byte value.

Fix

Type-inference only (type_analysis.rs). The no-encoding digest chain is refined to Uint8Array in the three places that classify it:

  • refine_type_from_init — the bound-local form (const buf = hash.digest()), the issue's repro.
  • static_type_of — the inline form (hash.digest().toString('hex') / hash.digest()[i]).
  • is_string_expr — so the toString(encoding) buffer path isn't skipped (it requires !is_string_expr(receiver)).

The encoded form (digest('hex'|'base64'|…)) stays String-typed, preserving the === / .length / .slice / .includes hex-string fast paths.

Validation

Byte-for-byte vs node --experimental-strip-types:

  • bound + inline digest().toString('hex') and digest().toString('base64') → correct
  • digest()[0] → numeric byte value
  • sha512 (runtime handle / fallback path) bound form → correct
  • Regression unchanged: encoded-chain a === b, .length, .slice, .includes, hmac ===, and typeof digest('base64') === 'string'.

Closes #1353

createHash(alg).update(d).digest() with no encoding arg returns a Buffer,
but the codegen typed every digest chain as String. So `const buf =
hash.digest(); buf.toString('hex')` treated the buffer as a Latin-1 string
and `buf[0]` returned a character instead of the byte value.

Refine the no-encoding digest chain to Uint8Array in all three places that
classify it — refine_type_from_init (bound local), static_type_of (inline
`digest().toString(...)` / `digest()[i]`), and is_string_expr (so the
buffer toString-with-encoding path isn't skipped). The encoded form
(digest('hex'|'base64'|...)) stays String-typed, preserving the
`===` / `.length` / `.slice` / `.includes` hex-string fast paths.

Verified vs `node --experimental-strip-types`: bound + inline
digest().toString('hex'|'base64') and digest()[i] now match, including the
sha512 fallback path; encoded-chain string ops unchanged.

Closes #1353
@proggeramlug proggeramlug merged commit 076c667 into main May 22, 2026
9 checks passed
@proggeramlug proggeramlug deleted the feat/crypto-digest-buffer-1353 branch May 22, 2026 14:31
proggeramlug added a commit that referenced this pull request May 22, 2026
…sweep (#1414)

Rolls up 26 PRs that merged to main post-v0.5.1023 without version
bumps:

- node:crypto gap-fixes (#1386 #1393 #1394 #1402 #1405): randomInt,
  timingSafeEqual, getHashes/getCiphers, sha224/sha384, base64 digest,
  Buffer hash input, no-arg digest() → Buffer, pbkdf2Sync digest arg,
  scryptSync.
- node:perf_hooks (#1321 + #1328 #1342 coverage): performance + User
  Timing + PerformanceObserver native impl, granular node-suite +
  edge-case coverage.
- #1090 GC checkpoint runtime work (#1324).
- #1311 geisterhand on iOS (#1316 #1383 #1384 #1385).
- #1312 process.env.X (unset) is nullish undefined (#1314).
- #1319 thread-safety hardening for cross-thread runtime statics.
- #1322 exact-head GC evidence packet.
- #1323 wasm timers dispatch through mem_call bridge (#1329).
- #1317 node:timers/promises shadow-segfault fix (#1326).
- #1330 node:process suite (#1331).
- #1292 bcrypt.hash() returns String (#1307).
- #1293 fastify .json()/.body external-fastify dispatch (#1308).
- #1296 app pattern performance gaps.
- #1297 diagnostics_channel parity.
- #1301 iOS App Groups capability (#1313).
- #1318 #1325 os/methods/modern-methods static dispatch.
- #1315 expanded Node parity test coverage.
- #1382 ui-ios stdlib pump for async fetch.
- #1392 ui-wasm reactive state + setText (#1404).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node:crypto — digest() Buffer does not round-trip Buffer#toString('hex')

1 participant