add support for v2 hashing algorithm.
update README.md and demo file to be more in sync. update to v0.10.0.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { blake2b } from "@exodus/blakejs";
|
||||
import { base32Encode } from "./utils.js";
|
||||
import { HASH_V2_EPOCH } from "./constants.js";
|
||||
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})([tT ])(\d{2}):(\d{2}):(\d{2})\.?(\d{3})?(?:(?:([+-]\d{2}):?(\d{2}))|Z)?$/;
|
||||
const formatRFC3339 = (date) => {
|
||||
const pad = (num = 0) => `${+num < 10 ? 0 : ""}${+num}`;
|
||||
@@ -28,10 +29,14 @@ const formatRFC3339 = (date) => {
|
||||
offset
|
||||
].join("");
|
||||
};
|
||||
function hashTwt(twt) {
|
||||
function hashTwt(twt, version = 0) {
|
||||
const created = formatRFC3339(twt.created);
|
||||
const payload = [twt.url, created, twt.content].join("\n");
|
||||
return base32Encode(blake2b(payload, void 0, 32)).toLowerCase().slice(-7);
|
||||
const encoded = base32Encode(blake2b(payload, void 0, 32)).toLowerCase();
|
||||
const createdDate = new Date(twt.created), epochDate = new Date(HASH_V2_EPOCH);
|
||||
if (version === 1 || !version && createdDate < epochDate)
|
||||
return encoded.slice(-7);
|
||||
return encoded.slice(0, 12);
|
||||
}
|
||||
export {
|
||||
hashTwt as default
|
||||
|
||||
Reference in New Issue
Block a user