Skip to content

Instantly share code, notes, and snippets.

@nom3ad
Last active January 2, 2017 18:16
Show Gist options
  • Save nom3ad/df08f11884a75baf006ad8466b109ecc to your computer and use it in GitHub Desktop.
Save nom3ad/df08f11884a75baf006ad8466b109ecc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import Crypto\n",
"from Crypto.PublicKey import RSA\n",
"from Crypto import Random\n",
"import base64\n",
"uuid = \"gh342454cgfdg675\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'022e5eaca87f6e79789b0f8b7e6e56e22ea5e6ef849a3c82ebee1698e3b7c036'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from Crypto.Hash import SHA256\n",
"sha=SHA256.new(uuid).hexdigest()\n",
"sha"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"random_generator = Random.new().read\n",
"txRSA = RSA.generate(1024, random_generator)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-----BEGIN RSA PRIVATE KEY-----\n",
"MIICWwIBAAKBgQC5rcfYyghHJkuqjRcPSPwD7eia/VTbAwiw3Skb8/vXn8SL6j/T\n",
"F8Uz33Is6Eh7o4Limxpy9D3SFblQ1tec/qjfCTCVqvGG/dN/NBV7NHRDpH+ah5+7\n",
"d9Ih77b9ZIlgOCVpbxV2a481TRG2vm2BOrgaSXnJ6FGf/Pz9DGMxU7IsqQIDAQAB\n",
"AoGAB6ZNMXVBWg0N2PzvEkYje9X1W01MjalWVlMGOoyWt49xfYItFvlHupdeENOR\n",
"COb3oXZaZJoDYgBbHiV7xJAJTPUO5mLiNsMk3E6cR1VFicYb9bz8HjG6NCHXtT8h\n",
"BkkHHBzicTvk/fuF8cF7Lmi2MmU842agZEphHz4vajJP78ECQQDTcs6Tfbg8lVIa\n",
"gt+kMZtMIiAFUDmj+aJo8RlajExkiOQdCKp6H2lshcjLyIzdQPYn0HAPeQn/KH48\n",
"xuqtIvelAkEA4M0AMM5LZwPSgdA+HmZmAhvhk/WzVeTWX1hfB39olgoEwI0qKDfe\n",
"CGAxgqyM1mRDSMsLYuqDhjq91W02LcCxtQJAGmA00oMZKz15G2N94C5r78hF9EO4\n",
"nPKzxqFkhz3+/X9x7Qk6srceYHIjxP/ESoki6359rABuNczoSk6J0YUOHQJAYv5j\n",
"oxp1Yfvw63GEqWZ2bV9RyxJb0ute68XCs06MA+aNiS2g7u0LPaz+oIjTzGWCq7+e\n",
"WATlvmqHxkC9pxBAMQJAIl3x5QLm/5sAnE08MT/HxYntDB9CwisBz9MvFhlEedDs\n",
"2+wA9D0PGVJbbM8M1fy6q83ZFs/77DITx+cXhKFREw==\n",
"-----END RSA PRIVATE KEY-----\n",
"-----BEGIN PUBLIC KEY-----\n",
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5rcfYyghHJkuqjRcPSPwD7eia\n",
"/VTbAwiw3Skb8/vXn8SL6j/TF8Uz33Is6Eh7o4Limxpy9D3SFblQ1tec/qjfCTCV\n",
"qvGG/dN/NBV7NHRDpH+ah5+7d9Ih77b9ZIlgOCVpbxV2a481TRG2vm2BOrgaSXnJ\n",
"6FGf/Pz9DGMxU7IsqQIDAQAB\n",
"-----END PUBLIC KEY-----\n"
]
}
],
"source": [
"print txRSA.exportKey() #privateky\n",
"pubkey=txRSA.publickey().exportKey()\n",
"print pubkey"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"69304172085658673512462670413590634318145262078752239615154669742911500851181615448230905250591930207084477242186294082021092600320825370619624463725254765098132589023616825590880058345117344957503846381820740276199277821955360553150967394253431906038500109998987947846113093098028913764537315941229262169734L"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sigL = txRSA.sign(sha,None)[0]\n",
"sigL"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def toByteArray(i):\n",
" b = bytearray()\n",
" while i:\n",
" b.append(i & 0xFF)\n",
" i >>= 8\n",
" return b[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" bytearray(b\"b\\xb1B=\\xe6\\xcf\\xd2\\xd5,\\xd4;n\\xd4\\xa7-vZ\\x15\\xee-_\\x95\\x93\\x00u\\xc9\\x9c\\x93\\xb6\\xf2\\xc3\\xa3LC\\xeck4D\\xbdw\\x07\\xb4\\xfc3k\\x05\\x82\\x8c\\x1ahi?\\xde\\x1e\\xef\\xe1\\x95\\x93cN\\x1d\\x84o\\xfe\\xb4\\xbe\\xd7}w5\\x02\\x99\\xd5\\xc8\\x00\\xed\\xf1\\xe1k+t`\\'|go\\x01\\x8dWv3(\\xd2\\xba\\xa7\\xd3j\\xe6M\\xa1!\\x86#\\x126\\xa6\\xa9R\\xd4\\x80\\x03\\xb5\\xf5(\\xda\\xdc\\xfd\\xd1\\xf3\\xd6\\x83\\xf4\\xb1\\xc1[Vf\\x86\")\n",
"YrFCPebP0tUs1Dtu1KctdloV7i1flZMAdcmck7byw6NMQ+xrNES9dwe0/DNrBYKMGmhpP94e7+GVk2NOHYRv/rS+1313NQKZ1cgA7fHhayt0YCd8Z28BjVd2MyjSuqfTauZNoSGGIxI2pqlS1IADtfUo2tz90fPWg/SxwVtWZoY= # 172\n"
]
}
],
"source": [
"ba = toByteArray(sigL)\n",
"print repr(ba)\n",
"Signature=base64.b64encode(ba)\n",
"print Signature,\" #\",len(Signature)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### in RX"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"rxuuid = uuid\n",
"rxSignature = Signature\n",
"\n",
"rxsha=SHA256.new(rxuuid).hexdigest()\n",
"\n",
"rxRSA = RSA.importKey(pubkey)\n",
"\n",
"def bytes_to_int(byte_array):\n",
" return int(byte_array.encode('hex'), 16)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"69304172085658673512462670413590634318145262078752239615154669742911500851181615448230905250591930207084477242186294082021092600320825370619624463725254765098132589023616825590880058345117344957503846381820740276199277821955360553150967394253431906038500109998987947846113093098028913764537315941229262169734\n"
]
}
],
"source": [
"rxSignatureL = bytes_to_int(base64.b64decode(rxSignature))\n",
"print rxSignatureL"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rxRSA.verify(rxsha,(rxSignatureL,))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment