Skip to content

Instantly share code, notes, and snippets.

@garethpaul
Created July 7, 2014 23:22
Show Gist options
  • Save garethpaul/3a1c584347b121361656 to your computer and use it in GitHub Desktop.
Save garethpaul/3a1c584347b121361656 to your computer and use it in GitHub Desktop.
import hashlib
import hmac
import base64
import argparse
def mact_hash(mact_id, secret_key):
"""Twitter's base hashing algorithm for MACT"""
return base64.b64encode(
hmac.new(secret_key, mact_id, hashlib.sha256).digest())
def test_mact_hash(mact_id, expected, secret_key):
"""Simple hash equality test."""
actual = mact_hash(mact_id, secret_key)
if actual == expected:
print 'Hashes match:'
else:
print 'Hashes do not match:'
print 'expected:', expected
print 'actual: ', actual
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Calculate MACT Hash.')
parser.add_argument('--key', dest='secret_key', required=True,
help='hmac secret key (base64)')
parser.add_argument('--id', dest='mact_id', required=True,
help='Google Ad ID, iOS IDFA, or sha1(Android ID)')
parser.add_argument('--expected', dest='expected',
required=False, help='The expected hash value.')
args = parser.parse_args()
secret_key = base64.b64decode(args.secret_key)
if args.expected:
test_mact_hash(args.mact_id, args.expected, secret_key)
else:
print mact_hash(args.mact_id, secret_key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment