Created
July 12, 2018 09:51
-
-
Save pdonorio/8a4015541891a2b0888a3cb0104333de to your computer and use it in GitHub Desktop.
Current failure on hash computing
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Authentication" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Following the instructions at:\n", | |
"https://cognitohq.com/docs/authenticating" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"import base64\n", | |
"import hashlib" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'{\"data\": {\"type\": \"profile\"}}'" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"body_dict = {\"data\": {\"type\": \"profile\"}}\n", | |
"body_str = json.dumps(body_dict)\n", | |
"body_str" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'280ffd96fd55bcb81f08b85f193a18bfe2c03c24ea037ef80f798e9341a17d8c'" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sha = hashlib.sha256(body_str.encode()).hexdigest()\n", | |
"sha_byte = sha.encode()\n", | |
"sha" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'MjgwZmZkOTZmZDU1YmNiODFmMDhiODVmMTkzYTE4YmZlMmMwM2MyNGVhMDM3ZWY4MGY3OThlOTM0MWExN2Q4Yw=='" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"base64.b64encode(sha_byte).decode()\n", | |
"# expected 0ScxH9+s7WbgysSzEMsD7xAq6Je0siYjdcVE6PSmpm4=" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"and I can't understand so far why it's not the same output as expected" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"---" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Also double checking all encodings available in base64:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"all_base64_methods = base64.__dict__.get('__all__')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"b64encode = MjgwZmZkOTZmZDU1YmNiODFmMDhiODVmMTkzYTE4YmZlMmMwM2MyNGVhMDM3ZWY4MGY3OThlOTM0MWExN2Q4Yw==\n", | |
"b32encode = GI4DAZTGMQ4TMZTEGU2WEY3CHAYWMMBYMI4DKZRRHEZWCMJYMJTGKMTDGAZWGMRUMVQTAMZXMVTDQMDGG44TQZJZGM2DCYJRG5SDQYY=\n", | |
"b16encode = 32383066666439366664353562636238316630386238356631393361313862666532633033633234656130333765663830663739386539333431613137643863\n", | |
"b85encode = GB_}1W@I@wW@I%rVq;=BF=jA0VmLKsF*!3~F*ssoWin$hGh;F|WnnNgH)UoxFlIM7IAu9AG%;Z@H)J?t\n", | |
"a85encode = 1,pt\"An3n[An3bV@Uik,0kN+!@Q65W0f_$u0fWWSAMRaL1Li0sARR8K2e?S\\0P37(3+Y*+1biDn2e4mX\n", | |
"standard_b64encode = MjgwZmZkOTZmZDU1YmNiODFmMDhiODVmMTkzYTE4YmZlMmMwM2MyNGVhMDM3ZWY4MGY3OThlOTM0MWExN2Q4Yw==\n", | |
"urlsafe_b64encode = MjgwZmZkOTZmZDU1YmNiODFmMDhiODVmMTkzYTE4YmZlMmMwM2MyNGVhMDM3ZWY4MGY3OThlOTM0MWExN2Q4Yw==\n" | |
] | |
} | |
], | |
"source": [ | |
"for method in all_base64_methods:\n", | |
" if method.endswith('encode'):\n", | |
" b64callable = getattr(base64, method)\n", | |
" try:\n", | |
" print(method, \"=\", b64callable(sha_byte).decode())\n", | |
" except TypeError:\n", | |
" # skipping methods with different parameters\n", | |
" pass" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment