Instantly share code, notes, and snippets.
CreatedNovember 11, 2022 14:30
Save hashrock/e003c7f023bfced46d97170e02ef65ae to your computer and use it in GitHub Desktop.
Deno twtxt hash calc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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"; | |
| functionbase32(payload:Uint8Array){ | |
| returnencode(payload).replace(/=/g,"").toLowerCase(); | |
| } | |
| functionblake2b256(payload:string){ | |
| returnblake2b(payload,undefined,32); | |
| } | |
| functionformatRFC3339(text:string){ | |
| returnDateTime.fromISO(text,{setZone:true,zone:"utc"}) | |
| .toFormat("yyyy-MM-dd'T'HH:mm:ssZZ") | |
| .replace(/\+00:00$/,"Z"); | |
| } | |
| consttwt={ | |
| url:"https://twtxt.net/user/prologic/twtxt.txt", | |
| hash:"o6dsrga", | |
| created:"2020-07-18T12:39:52Z", | |
| content:"Hello World! 😊", | |
| }; | |
| constcreated=formatRFC3339(twt.created); | |
| constpayload=[twt.url,created,twt.content].join("\n"); | |
| consthash=base32(blake2b256(payload)).slice(-7); | |
| Deno.test("hash",()=>{ | |
| assertEquals(hash,twt.hash); | |
| }); |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment