Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Strings are UTF-8. Already have bytes? Pass a `Uint8Array`. `create()` takes the

| Category | Algorithms | HMAC |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| cryptographic | sha256, sha384, sha512, sha224, sha512-224, sha512-256, sha512-half, sha3-256, sha3-512, shake128, shake256, keccak256, blake2b, blake2b-256, blake2b-224, blake2s, blake3, blake256, ripemd160, ripemd320, hash160, hash256, whirlpool, hkdf | sha2, sha3, keccak256, blake2b, blake2s, ripemd160, ripemd320, whirlpool |
| cryptographic | sha256, sha384, sha512, sha224, sha512-224, sha512-256, sha512-half, sha3-256, sha3-512, shake128, shake256, keccak256, blake2b, blake2b-256, blake2b-224, blake2s, blake3, blake256, ripemd160, ripemd320, hash160, sha256d, whirlpool, hkdf | sha2, sha3, keccak256, blake2b, blake2s, ripemd160, ripemd320, whirlpool |
| legacy | md5, md4, md2, ntlm, sha1, sha0, ripemd128, ripemd256, haval, tiger | all but ntlm |
| non-cryptographic | crc32, crc64, crc24, crc16-xmodem, adler32, xxhash, fnv1a | no |
| password | scrypt, pbkdf2, evp-bytestokey, argon2id, argon2i, argon2d, bcrypt | no, they take a salt |
Expand Down
2 changes: 1 addition & 1 deletion docs/app/composables/useLandingHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const ORDER: readonly AlgorithmEntry["slug"][] = [
"blake256",
"sha1",
"fnv1a",
"hash256",
"sha256d",
"blake2s",
"crc16-xmodem",
"sha384",
Expand Down
2 changes: 1 addition & 1 deletion docs/app/utils/algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const PRESENTATION: Record<BuiltinAlgorithm, { icon: string; blurb: string; used
blurb: "RIPEMD-160 of SHA-256, a public key becomes an address",
usedBy: "Bitcoin, Litecoin, Dogecoin, Dash, Zcash, XRP Ledger",
},
hash256: {
sha256d: {
icon: "i-token-btc",
blurb: "SHA-256 twice, every txid and block hash",
usedBy: "Bitcoin",
Expand Down
6 changes: 3 additions & 3 deletions docs/content/1.guide/02.hashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ sha256.hash("abc", { rounds: 2, chain: "hex" }).digest;

Same input, same count, two answers. Why? Round two has to hash something. `chain: "bytes"` is the default and hashes the raw digest. `chain: "hex"` hashes the lowercase text that spells it. Both recipes are out there. A page rarely says which one it means, so try both before you doubt your answer.

Does the first digest look familiar? It's `hash256`. Bitcoin has run two rounds of SHA-256 over bytes since 2009.
Does the first digest look familiar? It's `sha256d`. Bitcoin has run two rounds of SHA-256 over bytes since 2009.

Past one round, `options` in the result names `rounds` and `chain`. One logged result is enough to repeat it. Every fixed-length digest takes them. HMAC and the KDFs don't, since they have their own cost. More rounds there is an error, not a quiet single pass.

Expand Down Expand Up @@ -98,7 +98,7 @@ hmac(sha512, text.encode("Bitcoin seed"), seed);

Each one takes a `Uint8Array` and gives you a new one. Here's the lineup:

- Digests: `sha224`, `sha256`, `sha384`, `sha512`, `ripemd160`, `hash160`, `hash256`, `keccak256`, `sha3_256`, `sha3_512`, `blake256` and `blake2b`.
- Digests: `sha224`, `sha256`, `sha384`, `sha512`, `ripemd160`, `hash160`, `sha256d`, `keccak256`, `sha3_256`, `sha3_512`, `blake256` and `blake2b`.
- Checksums: `crc32`, `crc64`, `crc24`, `crc16Xmodem`, `adler32` and `xxhash`.
- Old formats that still ask for them: `sha1` and `md5`.

Expand Down Expand Up @@ -132,7 +132,7 @@ import { hmac } from "@agntn/hashes/hmac";
|---|---|
| `@agntn/hashes/sha1` | `sha1`, `Sha1Hasher` |
| `@agntn/hashes/md5` | `md5`, `Md5Hasher` |
| `@agntn/hashes/sha2` | `sha224`, `sha256`, `sha256Bits`, `sha384`, `sha512`, `hash256`, `Sha224Hasher`, `Sha256Hasher`, `Sha384Hasher`, `Sha512Hasher` |
| `@agntn/hashes/sha2` | `sha224`, `sha256`, `sha256Bits`, `sha384`, `sha512`, `sha256d`, `Sha224Hasher`, `Sha256Hasher`, `Sha384Hasher`, `Sha512Hasher` |
| `@agntn/hashes/ripemd160` | `ripemd160`, `hash160`, `Ripemd160Hasher` |
| `@agntn/hashes/keccak` | `keccak256`, `sha3_256`, `sha3_512`, `shake128`, `shake256`, `keccakF1600`, `Keccak256Hasher`, `Sha3_256Hasher`, `Sha3_512Hasher` |
| `@agntn/hashes/blake2b` | `blake2b`, `Blake2bHasher` |
Expand Down
2 changes: 1 addition & 1 deletion docs/content/2.algorithms/01.sha256.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ create("sha256").hash("The quick brown fox jumps over the lazy dog", { key: "key

The first two are the FIPS 180 vector and the empty string, the third is the HMAC one Wikipedia quotes. If a SHA-256 of `""` doesn't start with `e3b0c442`, something upstream added a newline.

Bitcoin runs it twice, that's [HASH256](/algorithms/hash256). Under RIPEMD-160 it becomes [HASH160](/algorithms/hash160).
Bitcoin runs it twice, that's [SHA256D](/algorithms/sha256d). Under RIPEMD-160 it becomes [HASH160](/algorithms/hash160).

## Messages that stop inside a byte

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
title: HASH256
title: SHA256D
description: SHA-256 twice. Bitcoin transaction ids and block hashes and the Base58Check checksum
---

::algorithm-facts{name="hash256"}
::algorithm-facts{name="sha256d"}
::

SHA-256 of SHA-256. Bitcoin's txids, block hashes, merkle nodes, and the 4-byte checksum at the end of every Base58Check string.

```ts
create("hash256").hash("hello").digest;
create("sha256d").hash("hello").digest;
// "9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50"
create("sha256").hash(create("sha256").hash("hello", { encoding: "binary" }).digest).digest;
// the same, spelled out
Expand Down
2 changes: 1 addition & 1 deletion extensions/shared/tool-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const HASH_CATEGORIES = [
export const BUILTIN_FAMILIES =
"SHA, Keccak, BLAKE, RIPEMD, Whirlpool, MD, HAVAL, Tiger, CRC, Adler, xxHash, FNV, scrypt, PBKDF, HKDF, OpenSSL, Argon2, bcrypt";
export const BUILTIN_ALGORITHMS =
"sha256, sha384, sha512, sha224, sha512-224, sha512-256, sha512-half, sha3-256, sha3-512, shake128, shake256, keccak256, blake2b, blake2b-256, blake2b-224, blake2s, blake3, blake256, ripemd160, ripemd320, hash160, hash256, whirlpool, md5, md4, md2, ntlm, sha1, sha0, ripemd128, ripemd256, haval, tiger, crc32, crc64, crc24, crc16-xmodem, adler32, xxhash, fnv1a, scrypt, pbkdf2, hkdf, evp-bytestokey, argon2id, argon2i, argon2d, bcrypt";
"sha256, sha384, sha512, sha224, sha512-224, sha512-256, sha512-half, sha3-256, sha3-512, shake128, shake256, keccak256, blake2b, blake2b-256, blake2b-224, blake2s, blake3, blake256, ripemd160, ripemd320, hash160, sha256d, whirlpool, md5, md4, md2, ntlm, sha1, sha0, ripemd128, ripemd256, haval, tiger, crc32, crc64, crc24, crc16-xmodem, adler32, xxhash, fnv1a, scrypt, pbkdf2, hkdf, evp-bytestokey, argon2id, argon2i, argon2d, bcrypt";
export const EXTENDABLE_ALGORITHMS =
"sha256, sha512, ripemd160, ripemd320, whirlpool, md5, md4, sha1, sha0, ripemd128, ripemd256, haval, tiger";
export const HMAC_ALGORITHMS =
Expand Down
6 changes: 3 additions & 3 deletions src/algorithms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Crc64 } from "./crc64.ts";
import { EvpBytesToKey } from "./evp-bytestokey.ts";
import { Fnv1a } from "./fnv1a.ts";
import { Hash160 } from "./hash160.ts";
import { Hash256 } from "./hash256.ts";
import { Sha256d } from "./sha256d.ts";
import { Haval } from "./haval.ts";
import { Hkdf } from "./hkdf.ts";
import { Keccak256 } from "./keccak256.ts";
Expand Down Expand Up @@ -74,7 +74,7 @@ export const builtins: readonly HashConstructor[] = [
Ripemd160,
Ripemd320,
Hash160,
Hash256,
Sha256d,
Whirlpool,
Md5,
Md4,
Expand Down Expand Up @@ -118,7 +118,7 @@ export {
EvpBytesToKey,
Fnv1a,
Hash160,
Hash256,
Sha256d,
Haval,
Hkdf,
Keccak256,
Expand Down
10 changes: 5 additions & 5 deletions src/algorithms/hash256.ts → src/algorithms/sha256d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FixedHash } from "../core/fixed-hash.ts";
import { hash256 } from "../core/sha2.ts";
import { sha256d } from "../core/sha2.ts";

export class Hash256 extends FixedHash {
static readonly key = "hash256";
export class Sha256d extends FixedHash {
static readonly key = "sha256d";
protected readonly about = {
label: "HASH256 (double SHA-256)",
label: "SHA256D (double SHA-256)",
description:
"SHA-256 of SHA-256, Bitcoin's transaction and block hash and the Base58Check checksum (its first 4 bytes)",
family: "SHA",
Expand All @@ -21,6 +21,6 @@ export class Hash256 extends FixedHash {
* @returns {Uint8Array} The 32-byte hash.
*/
protected digest(bytes: Uint8Array): Uint8Array {
return hash256(bytes);
return sha256d(bytes);
}
}
2 changes: 1 addition & 1 deletion src/core/algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const builtinAlgorithms = [
"ripemd160",
"ripemd320",
"hash160",
"hash256",
"sha256d",
/** Cryptographic (Whirlpool) */
"whirlpool",
// Legacy
Expand Down
2 changes: 1 addition & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export {
Sha256Hasher,
Sha384Hasher,
Sha512Hasher,
hash256,
sha256d,
sha224,
sha256,
sha256Bits,
Expand Down
2 changes: 1 addition & 1 deletion src/core/sha2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,6 @@ export function sha512(data: Uint8Array): Uint8Array {
* @param data - Bytes to hash.
* @returns {Uint8Array} 32 bytes.
*/
export function hash256(data: Uint8Array): Uint8Array {
export function sha256d(data: Uint8Array): Uint8Array {
return sha256(sha256(data));
}
2 changes: 1 addition & 1 deletion src/sha2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export {
Sha256Hasher,
Sha384Hasher,
Sha512Hasher,
hash256,
sha256d,
sha224,
sha256,
sha256Bits,
Expand Down
14 changes: 7 additions & 7 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
evpBytesToKey,
has,
hash160,
hash256,
sha256d,
hashCategories,
hkdf,
hkdfExpand,
Expand Down Expand Up @@ -174,7 +174,7 @@ describe("registry", () => {
"sha3-512",
"shake128",
"shake256",
"hash256",
"sha256d",
"sha1",
"sha0",
]);
Expand Down Expand Up @@ -480,7 +480,7 @@ describe("digests", () => {
"blake3",
"blake256",
"hash160",
"hash256",
"sha256d",
"ntlm",
"crc32",
"crc64",
Expand Down Expand Up @@ -730,12 +730,12 @@ describe("hashes chains use", () => {
expect(create("hash160").hash(key).digest).toBe("751e76e8199196d454941c45d1b3a323f1433bd6");
});

it("hash256 gives the genesis block hash, byte-reversed as explorers print it", () => {
it("sha256d gives the genesis block hash, byte-reversed as explorers print it", () => {
const header = Buffer.from(
"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c",
"hex",
);
const digest = create("hash256").hash(header, { encoding: "binary" }).digest as Uint8Array;
const digest = create("sha256d").hash(header, { encoding: "binary" }).digest as Uint8Array;
expect(Buffer.from(digest).reverse().toString("hex")).toBe(
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
);
Expand Down Expand Up @@ -1263,7 +1263,7 @@ describe("byte functions", () => {
expect(sha512(bytes).toHex()).toBe(createHash("sha512").update(bytes).digest("hex"));
expect(ripemd160(bytes).toHex()).toBe(createHash("ripemd160").update(bytes).digest("hex"));
expect(hash160(bytes).toHex()).toBe(createHash("ripemd160").update(inner).digest("hex"));
expect(hash256(bytes).toHex()).toBe(createHash("sha256").update(inner).digest("hex"));
expect(sha256d(bytes).toHex()).toBe(createHash("sha256").update(inner).digest("hex"));
expect(keccak256(bytes).toHex()).toBe(createHash("keccak-256").update(bytes).digest("hex"));
expect(sha3_256(bytes).toHex()).toBe(createHash("sha3-256").update(bytes).digest("hex"));
expect(sha3_512(bytes).toHex()).toBe(createHash("sha3-512").update(bytes).digest("hex"));
Expand Down Expand Up @@ -2033,7 +2033,7 @@ describe("byte function subpaths", () => {
"Sha256Hasher",
"Sha384Hasher",
"Sha512Hasher",
"hash256",
"sha256d",
"sha224",
"sha256",
"sha256Bits",
Expand Down
Loading