Created
June 28, 2016 15:54
-
-
Save lukecampbell/c8565d981897322cb66a18f31bd3c695 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{"nbformat_minor": 0, "cells": [{"execution_count": 1, "cell_type": "code", "source": "import hashlib\nimport os\nimport binascii\ndef hash_plaintext(password, salt=None):\n if salt is None or len(salt) < 8:\n salt = os.urandom(8)\n salt = salt[:8]\n hashed = hashlib.pbkdf2_hmac('sha256', password, salt, 100000)\n hashed_password = salt + hashed\n return binascii.hexlify(hashed_password)", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": 6, "cell_type": "code", "source": "password = 'toomanysecrets'\nhashed_password = hash_plaintext(password)\nraw_hash = binascii.unhexlify(hashed_password)\ncheck = 'toomanysecrets'\nsalt = raw_hash[:8]\nhashed_check = hash_plaintext(check, salt)\n\nhashed_password == hashed_check", "outputs": [{"execution_count": 6, "output_type": "execute_result", "data": {"text/plain": "True"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.10", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment