Browse Source

Fix Python twt hash reference implementation (#459)

The spec says:

> If the timestamp does not explicitly include any timezone information,
> it must be assumed to be in UTC.
>
> […]
> 2020-12-13T07:45:23       → 2020-12-13T07:45:23Z

This commit addresses the UTC fallback. Previously, when no timezone was
present, the Zulu timezone was not used when calculating the twt hash,
producing a different twt hash.

In [`tt`](https://git.isobeef.org/lyse/tt/) I have [a test case verifying](https://git.isobeef.org/lyse/tt/-/blob/master/test_twtxthash.py#L51) this [equivalent but more verbose and commented implementation](https://git.isobeef.org/lyse/tt/-/blob/master/twtxthash.py#L41).

Co-authored-by: Lysander Trischler <twtxt@lyse.isobeef.org>
Reviewed-on: #459
Reviewed-by: James Mills <james@mills.io>
Co-authored-by: lyse <lyse@noreply@mills.io>
Co-committed-by: lyse <lyse@noreply@mills.io>
master
lyse 3 days ago
committed by James Mills
parent
commit
b3cf32037f
  1. 5
      docs/_posts/2020-12-11-twthashextension.md

5
docs/_posts/2020-12-11-twthashextension.md

@ -121,7 +121,10 @@ hash = hash[len(hash)-7:]
### Python 3
```python
created = twt.created.isoformat().replace("+00:00", "Z")
created = twt.created
if created.tzinfo is None:
created = created.replace(tzinfo=datetime.timezone.utc)
created = created.isoformat().replace("+00:00", "Z")
payload = "%s\n%s\n%s" % (twt.twter.url, created, twt.text)
sum256 = hashlib.blake2b(payload.encode("utf-8"), digest_size=32).digest()
hash = base64.b32encode(sum256).decode("ascii").replace("=", "").lower()[-7:]

Loading…
Cancel
Save