forked from cactus/go-camo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
python3-base64.py
43 lines (30 loc) · 1.14 KB
/
python3-base64.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright (c) 2012-2023 Eli Janssen
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file.
import hashlib
import hmac
import base64
CAMO_HOST = 'https://img.example.com'
def wrap_encode(data):
"""A little helper method to wrap b64encoding"""
return base64.urlsafe_b64encode(data).strip(b'=').decode('utf-8')
def camo_url(hmac_key, image_url):
if image_url.startswith("https:"):
return image_url
hmac_key = hmac_key.encode() if isinstance(hmac_key, str) else hmac_key
image_url = image_url.encode() if isinstance(image_url, str) else image_url
# setup the hmac construction
mac = hmac.new(hmac_key, digestmod=hashlib.sha1)
# add image_url
mac.update(image_url)
# generate digest
digest = mac.digest()
## now build url
b64digest = wrap_encode(digest)
b64url = wrap_encode(image_url)
requrl = '%s/%s/%s' % (CAMO_HOST, b64digest, b64url)
return requrl
print(
camo_url("test", "http://golang.org/doc/gopher/frontpage.png")
)
# https://img.example.org/D23vHLFHsOhPOcvdxeoQyAJTpvM/aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n