Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created November 11, 2022 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashrock/e003c7f023bfced46d97170e02ef65ae to your computer and use it in GitHub Desktop.
Save hashrock/e003c7f023bfced46d97170e02ef65ae to your computer and use it in GitHub Desktop.
Deno twtxt hash calc
import { encode } from "https://deno.land/std@0.163.0/encoding/base32.ts";
import { blake2b } from "https://esm.sh/blakejs@1.2.1";
import { DateTime } from "https://esm.sh/luxon@3.1.0";
import { assertEquals } from "https://deno.land/std@0.163.0/testing/asserts.ts";
function base32(payload: Uint8Array) {
return encode(payload).replace(/=/g, "").toLowerCase();
}
function blake2b256(payload: string) {
return blake2b(payload, undefined, 32);
}
function formatRFC3339(text: string) {
return DateTime.fromISO(text, { setZone: true, zone: "utc" })
.toFormat("yyyy-MM-dd'T'HH:mm:ssZZ")
.replace(/\+00:00$/, "Z");
}
const twt = {
url: "https://twtxt.net/user/prologic/twtxt.txt",
hash: "o6dsrga",
created: "2020-07-18T12:39:52Z",
content: "Hello World! 😊",
};
const created = formatRFC3339(twt.created);
const payload = [twt.url, created, twt.content].join("\n");
const hash = base32(blake2b256(payload)).slice(-7);
Deno.test("hash", () => {
assertEquals(hash, twt.hash);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment