-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drop some of the examples, as the hex examples are not much different than the base64 examples, and are easy to extrapolate.
- Loading branch information
Showing
17 changed files
with
104 additions
and
138 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) 2012-2019 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 |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.