Skip to content

Instantly share code, notes, and snippets.

@amritghimire
Created January 31, 2023 07:23
Show Gist options
  • Save amritghimire/24d96ec485b609a54fbf61c652e301d8 to your computer and use it in GitHub Desktop.
Save amritghimire/24d96ec485b609a54fbf61c652e301d8 to your computer and use it in GitHub Desktop.
licensing.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOXyMyuBcVlz/aPOCXsmkst",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/amritghimire/24d96ec485b609a54fbf61c652e301d8/licensing.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LXGPrm38e3HT",
"outputId": "aebe224e-8674-4473-c47f-c706eb9c05d0"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Requirement already satisfied: cryptography in /usr/local/lib/python3.8/dist-packages (39.0.0)\n",
"Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.8/dist-packages (from cryptography) (1.15.1)\n",
"Requirement already satisfied: pycparser in /usr/local/lib/python3.8/dist-packages (from cffi>=1.12->cryptography) (2.21)\n"
]
}
],
"source": [
"!pip install cryptography"
]
},
{
"cell_type": "markdown",
"source": [
"## Getting a key "
],
"metadata": {
"id": "sfXPnvlAhQuE"
}
},
{
"cell_type": "code",
"source": [
"from cryptography.hazmat.backends import default_backend\n",
"from cryptography.hazmat.primitives.asymmetric import rsa\n",
"from cryptography.hazmat.primitives import serialization\n"
],
"metadata": {
"id": "2JoLornSfMc8"
},
"execution_count": 86,
"outputs": []
},
{
"cell_type": "code",
"source": [
"private_key = rsa.generate_private_key(\n",
" public_exponent=65537,\n",
" key_size=2048,\n",
" backend=default_backend()\n",
")\n",
"public_key = private_key.public_key()"
],
"metadata": {
"id": "U0mo1kUNfPQ2"
},
"execution_count": 87,
"outputs": []
},
{
"cell_type": "code",
"source": [
"private_key"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "a2MHQZQPfRfz",
"outputId": "9c71909c-e586-4500-887c-d8acbb6ba2a5"
},
"execution_count": 88,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<cryptography.hazmat.backends.openssl.rsa._RSAPrivateKey at 0x7efe23ff6ac0>"
]
},
"metadata": {},
"execution_count": 88
}
]
},
{
"cell_type": "code",
"source": [
"private_pem = private_key.private_bytes(\n",
" encoding=serialization.Encoding.PEM,\n",
" format=serialization.PrivateFormat.PKCS8,\n",
" encryption_algorithm=serialization.NoEncryption()\n",
") # Private key "
],
"metadata": {
"id": "Lt36G1zBfTkO"
},
"execution_count": 89,
"outputs": []
},
{
"cell_type": "code",
"source": [
"private_pem # Save this private key to vault or somewhere "
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gFXMOn5qfaxV",
"outputId": "459966a9-ee3e-410e-aa35-acc57954c7ed"
},
"execution_count": 90,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC3QpoxVv8NpWIc\\n/utBZpoRLu40OhSKgnXsjMytLzKtotCYlZDah8CkGmhcO+t1LZZGfCxqkVkzxmp2\\nYs+MuhMlwwBQYFmhZLh4oB3BDQCBCJEk0Vew8EcTcP76dTZFiQCr+z81kedaSBWR\\nGAzj5o5MFnParOTvqHwY1lIVHM0F/TfPVWkPRhVfYNkZqrJgTlBZc8E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T\\nxGcB7y14L7wJIeWa7Py/rVBoNsueR1R5GPaZsFoYOnVZdb40peDG8xsVwd+wneDk\\nHsn4HXETI8OaP52+S5BX0OYhAWt6LobnYX6+CXa+9FyUZE0nSkqb6Ifx423VmQGs\\nYPwkNGaSxT5parOwUTwINVZWJUKmO7wBy1Ckb3kCgYEAuKv0yiNxCnc6IfCsnquH\\n2hEC5Io258iekE0gdm21am2p+O/ACfcLcjfIx77xqOciy3BZRDobvhxXyyPCgx9H\\n+8VIEhm9JqtpMZx7Pn9e5n9XNmpyf29y7nt0a09yWvmI7q5iEdWv9XgD/dz6CotY\\ntzN64DOVUM3Eih20LVmp7OE=\\n-----END PRIVATE KEY-----\\n'"
]
},
"metadata": {},
"execution_count": 90
}
]
},
{
"cell_type": "code",
"source": [
"public_pem = public_key.public_bytes(\n",
" encoding=serialization.Encoding.PEM,\n",
" format=serialization.PublicFormat.SubjectPublicKeyInfo\n",
") # Public key "
],
"metadata": {
"id": "StHfhwS5fl-x"
},
"execution_count": 91,
"outputs": []
},
{
"cell_type": "code",
"source": [
"public_pem # This will be passed as env variable or read from a file for self hosted env. It can be specific for each self hosted environment if we want"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gEo4a03-f4T-",
"outputId": "13e10775-2a99-4ca7-af46-1c8a9a6a5ee9"
},
"execution_count": 92,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt0KaMVb/DaViHP7rQWaa\\nES7uNDoUioJ17IzMrS8yraLQmJWQ2ofApBpoXDvrdS2WRnwsapFZM8ZqdmLPjLoT\\nJcMAUGBZoWS4eKAdwQ0AgQiRJNFXsPBHE3D++nU2RYkAq/s/NZHnWkgVkRgM4+aO\\nTBZz2qzk76h8GNZSFRzNBf03z1VpD0YVX2DZGaqyYE5QWXPBGXh9PXmNlzSoN/tl\\nULhw6eq+LVDtc1lsrBSajoDxHmmvwIg/2pF3CJJSNzgjqQGjHthifQftGsX+jm2O\\nUa7cWMXjbDThg2dwi/BscFXRvsq9AXKoFq9bl2oSttOL0cDWUkKEfENwHkKfeuSB\\nFQIDAQAB\\n-----END PUBLIC KEY-----\\n'"
]
},
"metadata": {},
"execution_count": 92
}
]
},
{
"cell_type": "code",
"source": [
"secret_message = {\n",
" \"customer_name\": \"ABC Company\",\n",
" \"pricing_per_seat\": \"50\",\n",
" \"purchased_seat\": 50\n",
"}"
],
"metadata": {
"id": "X5Bxc4tKf_zE"
},
"execution_count": 93,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import json\n",
"payload = json.dumps(secret_message)\n",
"payload"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "hc9cFFJ5gK-_",
"outputId": "cb2619ca-dd9e-4300-9dfc-4a6ff19ae331"
},
"execution_count": 94,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{\"customer_name\": \"ABC Company\", \"pricing_per_seat\": \"50\", \"purchased_seat\": 50}'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 94
}
]
},
{
"cell_type": "markdown",
"source": [
"### Signing the message"
],
"metadata": {
"id": "LPfxyQr7hJSD"
}
},
{
"cell_type": "code",
"source": [
"## Encrypt the message\n",
"### This could be made automated as well"
],
"metadata": {
"id": "ZBHwe4IOgmWP"
},
"execution_count": 95,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from cryptography.hazmat.primitives import hashes\n",
"from cryptography.hazmat.primitives.asymmetric import padding\n",
"import base64"
],
"metadata": {
"id": "kj3HJZuhgunY"
},
"execution_count": 96,
"outputs": []
},
{
"cell_type": "code",
"source": [
"encoded_message = base64.b64encode(bytes(payload, \"UTF-8\"))\n",
"encoded_message # We can pass this as env variable"
],
"metadata": {
"id": "Q-JDaUAYhEER",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8eb71738-25da-4e68-c70e-65231240e878"
},
"execution_count": 97,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'eyJjdXN0b21lcl9uYW1lIjogIkFCQyBDb21wYW55IiwgInByaWNpbmdfcGVyX3NlYXQiOiAiNTAiLCAicHVyY2hhc2VkX3NlYXQiOiA1MH0='"
]
},
"metadata": {},
"execution_count": 97
}
]
},
{
"cell_type": "code",
"source": [
"base64.b64decode(encoded_message)"
],
"metadata": {
"id": "QrJ9YDrEksWy",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "242152ad-a58b-4f9f-ff84-85ac34f3ad19"
},
"execution_count": 98,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'{\"customer_name\": \"ABC Company\", \"pricing_per_seat\": \"50\", \"purchased_seat\": 50}'"
]
},
"metadata": {},
"execution_count": 98
}
]
},
{
"cell_type": "code",
"source": [
"signature = base64.b64encode(\n",
" private_key.sign(\n",
" encoded_message,\n",
" padding.PSS(\n",
" mgf = padding.MGF1(hashes.SHA256()),\n",
" salt_length = padding.PSS.MAX_LENGTH,\n",
" ),\n",
" hashes.SHA256(),\n",
" )\n",
")"
],
"metadata": {
"id": "wE4EtJm0hLM-"
},
"execution_count": 99,
"outputs": []
},
{
"cell_type": "code",
"source": [
"signature"
],
"metadata": {
"id": "PN5KdPW4jmix",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "813129af-bc6b-4e6a-a42c-f12a9f3f7002"
},
"execution_count": 100,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'NVtjnHsX6AfxupJcI7kPw6aifj75pX7LwE+IPgPCjhiNBqowWgHJTnyIM8eQUjtZ7MU136WRGFXL1PpKOfDWM9vEFjDtDtJ7Lnpsbk80QE11BKUknkYlmKvrX5CH3j6qKBAOPdH5iS7yBtBTKugAiWcbKNIUBELRe1pVqnbaFh9qb/50neeoErMDTyL8oPLhQXZC1u5h5MPYyqnDlSb8jog8u8wgs/ofKlCVUk/0gNDEHkdtwAJ58SBLucRu2xgZDMLzrk+ozoB5ukZJdmM+7BVnaxtYd1AEM47iPhF1XyK7wg1956mjCsqFuKJ1es2wSuTTUQ9f2ftO8vH2JQV3RQ=='"
]
},
"metadata": {},
"execution_count": 100
}
]
},
{
"cell_type": "markdown",
"source": [
"So, basically we now have public key, signature and encoded_message to pass in Self-hosted environment"
],
"metadata": {
"id": "3jS-Xt8Ihatx"
}
},
{
"cell_type": "code",
"source": [
"license_information = b' '.join([encoded_message, signature])\n",
"license_information"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eqU0OBY-hx8Y",
"outputId": "698ef40b-a7db-415e-cfc2-8fb27350d346"
},
"execution_count": 101,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'eyJjdXN0b21lcl9uYW1lIjogIkFCQyBDb21wYW55IiwgInByaWNpbmdfcGVyX3NlYXQiOiAiNTAiLCAicHVyY2hhc2VkX3NlYXQiOiA1MH0= NVtjnHsX6AfxupJcI7kPw6aifj75pX7LwE+IPgPCjhiNBqowWgHJTnyIM8eQUjtZ7MU136WRGFXL1PpKOfDWM9vEFjDtDtJ7Lnpsbk80QE11BKUknkYlmKvrX5CH3j6qKBAOPdH5iS7yBtBTKugAiWcbKNIUBELRe1pVqnbaFh9qb/50neeoErMDTyL8oPLhQXZC1u5h5MPYyqnDlSb8jog8u8wgs/ofKlCVUk/0gNDEHkdtwAJ58SBLucRu2xgZDMLzrk+ozoB5ukZJdmM+7BVnaxtYd1AEM47iPhF1XyK7wg1956mjCsqFuKJ1es2wSuTTUQ9f2ftO8vH2JQV3RQ=='"
]
},
"metadata": {},
"execution_count": 101
}
]
},
{
"cell_type": "markdown",
"source": [
"We will pass **Public Key** (`public_pem`) and **License Information** (`license_information` ) as a environment variable "
],
"metadata": {
"id": "rPYYQtXLhn_3"
}
},
{
"cell_type": "markdown",
"source": [
"## Verifying the signature, This will be on Studio Code"
],
"metadata": {
"id": "gnXWIWbTi8R7"
}
},
{
"cell_type": "code",
"source": [
"public_pem # Read from env variable or file"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dDR49nngjjZV",
"outputId": "cd205aad-0482-46cd-e076-05a32f9a077b"
},
"execution_count": 102,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt0KaMVb/DaViHP7rQWaa\\nES7uNDoUioJ17IzMrS8yraLQmJWQ2ofApBpoXDvrdS2WRnwsapFZM8ZqdmLPjLoT\\nJcMAUGBZoWS4eKAdwQ0AgQiRJNFXsPBHE3D++nU2RYkAq/s/NZHnWkgVkRgM4+aO\\nTBZz2qzk76h8GNZSFRzNBf03z1VpD0YVX2DZGaqyYE5QWXPBGXh9PXmNlzSoN/tl\\nULhw6eq+LVDtc1lsrBSajoDxHmmvwIg/2pF3CJJSNzgjqQGjHthifQftGsX+jm2O\\nUa7cWMXjbDThg2dwi/BscFXRvsq9AXKoFq9bl2oSttOL0cDWUkKEfENwHkKfeuSB\\nFQIDAQAB\\n-----END PUBLIC KEY-----\\n'"
]
},
"metadata": {},
"execution_count": 102
}
]
},
{
"cell_type": "code",
"source": [
"license_information # Read from env variable or file"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8Y4vt19HjqDy",
"outputId": "1001996a-fe15-4066-8ca9-7729370a6223"
},
"execution_count": 103,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'eyJjdXN0b21lcl9uYW1lIjogIkFCQyBDb21wYW55IiwgInByaWNpbmdfcGVyX3NlYXQiOiAiNTAiLCAicHVyY2hhc2VkX3NlYXQiOiA1MH0= NVtjnHsX6AfxupJcI7kPw6aifj75pX7LwE+IPgPCjhiNBqowWgHJTnyIM8eQUjtZ7MU136WRGFXL1PpKOfDWM9vEFjDtDtJ7Lnpsbk80QE11BKUknkYlmKvrX5CH3j6qKBAOPdH5iS7yBtBTKugAiWcbKNIUBELRe1pVqnbaFh9qb/50neeoErMDTyL8oPLhQXZC1u5h5MPYyqnDlSb8jog8u8wgs/ofKlCVUk/0gNDEHkdtwAJ58SBLucRu2xgZDMLzrk+ozoB5ukZJdmM+7BVnaxtYd1AEM47iPhF1XyK7wg1956mjCsqFuKJ1es2wSuTTUQ9f2ftO8vH2JQV3RQ=='"
]
},
"metadata": {},
"execution_count": 103
}
]
},
{
"cell_type": "code",
"source": [
"from cryptography.hazmat.primitives.serialization import load_pem_public_key\n",
"from cryptography.hazmat.backends import default_backend\n"
],
"metadata": {
"id": "JdVx3Snsjfdw"
},
"execution_count": 104,
"outputs": []
},
{
"cell_type": "code",
"source": [
"public_key = load_pem_public_key(public_pem, default_backend())"
],
"metadata": {
"id": "vzdm8152jA_K"
},
"execution_count": 105,
"outputs": []
},
{
"cell_type": "code",
"source": [
"payload, signature = license_information.split(b' ', 1)"
],
"metadata": {
"id": "WRt16VjpkcB-"
},
"execution_count": 106,
"outputs": []
},
{
"cell_type": "code",
"source": [
"public_key.verify(\n",
" base64.b64decode(signature),\n",
" payload,\n",
" padding.PSS(\n",
" mgf = padding.MGF1(hashes.SHA256()),\n",
" salt_length = padding.PSS.MAX_LENGTH,\n",
" ),\n",
" hashes.SHA256(),\n",
") # This will raise cryptography.exceptions.InvalidSignature in case of well invalid signature"
],
"metadata": {
"id": "-qq8BxAXlPNL"
},
"execution_count": 107,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Moving into functions"
],
"metadata": {
"id": "SynUR97FmAS7"
}
},
{
"cell_type": "code",
"source": [
"def get_key_pair():\n",
" \"\"\"\n",
" Used to create a new key pair.\n",
" \"\"\"\n",
" from cryptography.hazmat.backends import default_backend\n",
" from cryptography.hazmat.primitives.asymmetric import rsa\n",
" from cryptography.hazmat.primitives import serialization\n",
"\n",
" private_key = rsa.generate_private_key(\n",
" public_exponent=65537,\n",
" key_size=2048,\n",
" backend=default_backend()\n",
" )\n",
" public_key = private_key.public_key()\n",
"\n",
" private_pem = private_key.private_bytes(\n",
" encoding=serialization.Encoding.PEM,\n",
" format=serialization.PrivateFormat.PKCS8,\n",
" encryption_algorithm=serialization.NoEncryption()\n",
" ) # Private key \n",
"\n",
" public_pem = public_key.public_bytes(\n",
" encoding=serialization.Encoding.PEM,\n",
" format=serialization.PublicFormat.SubjectPublicKeyInfo\n",
" ) # Public key \n",
"\n",
" return private_pem, public_pem"
],
"metadata": {
"id": "tc8KrGXkrXhJ"
},
"execution_count": 109,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def sign_payload(pem, payload):\n",
" \"\"\"\n",
" This is used to create the license file or variable from the private key.\n",
" It is stored as a util outside of studio.\n",
" \"\"\"\n",
" from cryptography.hazmat.primitives import hashes\n",
" from cryptography.hazmat.primitives.asymmetric import padding\n",
" from cryptography.hazmat.primitives import serialization\n",
" from cryptography.hazmat.backends import default_backend\n",
" import base64\n",
" import json\n",
" \n",
" private_key = serialization.load_pem_private_key(\n",
" pem,\n",
" password = None,\n",
" backend = default_backend(),\n",
" )\n",
" encoded_message = base64.b64encode(bytes(json.dumps(payload), \"UTF-8\"))\n",
" signature = base64.b64encode(\n",
" private_key.sign(\n",
" encoded_message,\n",
" padding.PSS(\n",
" mgf = padding.MGF1(hashes.SHA256()),\n",
" salt_length = padding.PSS.MAX_LENGTH,\n",
" ),\n",
" hashes.SHA256(),\n",
" )\n",
" )\n",
" license_information = b' '.join([encoded_message, signature])\n",
" return license_information"
],
"metadata": {
"id": "_m7S_ygCme64"
},
"execution_count": 110,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def verify_payload(pem, license):\n",
" # This will be part of studio.\n",
"\n",
"\n",
" from cryptography.hazmat.primitives.serialization import load_pem_public_key\n",
" from cryptography.hazmat.backends import default_backend\n",
" from cryptography.hazmat.primitives import hashes\n",
" from cryptography.hazmat.primitives.asymmetric import padding\n",
" import base64\n",
" import json\n",
"\n",
" public_key = load_pem_public_key(pem, default_backend())\n",
" payload, signature = license.split(b' ', 1)\n",
"\n",
" public_key.verify(\n",
" base64.b64decode(signature),\n",
" payload,\n",
" padding.PSS(\n",
" mgf = padding.MGF1(hashes.SHA256()),\n",
" salt_length = padding.PSS.MAX_LENGTH,\n",
" ),\n",
" hashes.SHA256(),\n",
" ) # This will raise cryptography.exceptions.InvalidSignature in case of well invalid signature\n",
"\n",
" json_payload = base64.b64decode(payload)\n",
" return json.loads(json_payload)"
],
"metadata": {
"id": "_sv2Uu_rpthh"
},
"execution_count": 117,
"outputs": []
},
{
"cell_type": "code",
"source": [
"private_pem, public_pem = get_key_pair() # Use this only once "
],
"metadata": {
"id": "PeNLh8h6ren4"
},
"execution_count": 112,
"outputs": []
},
{
"cell_type": "code",
"source": [
"private_pem, public_pem"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PvKugP3osQ_7",
"outputId": "9b849a9c-05ea-4620-fcb7-8137d5834de1"
},
"execution_count": 113,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(b'-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC/Oi5szN5OuXXf\\nZ7xsR8gDZ8PATPlx7pM8JGmP29MzGlyenopECQBC3SxLRisw9Ie6FBRrxOdkpBIg\\nENvAAKoJFQBzWcy7GFtTrJy1YEb6YatGcF8h/aQuQ/AVgmzgscNh2/RC33vLBMfI\\nkCsLEzqLo03EsUlrLpaYd83/rVkWIiUNxtHZPuMQIOs//fInRglEOACnoI/ennH3\\nSkn2gtDAfptLiG2sMlQHH0TMVy1TxuCM4frwVenb9TuN0xHLCXr7cfga4uOC+XLK\\noVPgjUmgaLk8/YmKp70PYXJHpKL/XLaoGLcocLfFl5mS/Qtb35i/rmWbyc4QBKBe\\nlbcJ3O+TAgMBAAECggEAEngSacxeSujbUJNv/bPvxnHBfz4v+BKxwmTwk8YIdO6+\\niUyzT/Tf7eqh+jcxuWMKBNfRom2n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tUtOeayKxikRCxiIDB6sbR\\nD+2GRbUy7c62MMBb7EDGF44MvlhgEE8U0Fy5Dcgmhpaant67p74a7gLxZPO5ZQCK\\nxb5dpn2+yL+vdXjCeMuTqRY=\\n-----END PRIVATE KEY-----\\n',\n",
" b'-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvzoubMzeTrl132e8bEfI\\nA2fDwEz5ce6TPCRpj9vTMxpcnp6KRAkAQt0sS0YrMPSHuhQUa8TnZKQSIBDbwACq\\nCRUAc1nMuxhbU6yctWBG+mGrRnBfIf2kLkPwFYJs4LHDYdv0Qt97ywTHyJArCxM6\\ni6NNxLFJay6WmHfN/61ZFiIlDcbR2T7jECDrP/3yJ0YJRDgAp6CP3p5x90pJ9oLQ\\nwH6bS4htrDJUBx9EzFctU8bgjOH68FXp2/U7jdMRywl6+3H4GuLjgvlyyqFT4I1J\\noGi5PP2Jiqe9D2FyR6Si/1y2qBi3KHC3xZeZkv0LW9+Yv65lm8nOEASgXpW3Cdzv\\nkwIDAQAB\\n-----END PUBLIC KEY-----\\n')"
]
},
"metadata": {},
"execution_count": 113
}
]
},
{
"cell_type": "code",
"source": [
"secret_message = {\n",
" \"customer_name\": \"ABC Company\",\n",
" \"pricing_per_seat\": \"50\",\n",
" \"purchased_seat\": 50\n",
"}"
],
"metadata": {
"id": "nnU47o40qUuq"
},
"execution_count": 114,
"outputs": []
},
{
"cell_type": "code",
"source": [
"license_information = sign_payload(private_pem, secret_message)\n",
"license_information"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JhBybCmwqqnv",
"outputId": "ecf3f535-7f48-4d12-9949-cf8226745357"
},
"execution_count": 115,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'eyJjdXN0b21lcl9uYW1lIjogIkFCQyBDb21wYW55IiwgInByaWNpbmdfcGVyX3NlYXQiOiAiNTAiLCAicHVyY2hhc2VkX3NlYXQiOiA1MH0= apfHT1Wx4kDm+OSTcfGUTBdhl830Z+fV2o+a15NOXrK/B6smkw4oI62BMkHPuUC9PuKSU6SiWESjjoKmpP0VzeJbRVlRpSlUVuGeiPzeTBUldALUGSzj4qd8qTlildNq8Am0E2KAsTN0t4AivDW2nTBYgIgtP5+a962eqMzBcpPaNqBTbRdmPeUuhxNMfsmb3JSBnmpQ55T8AkVFQg0wT6QHSqavncz6nEWbUg91F5ePEuaF+FuHsk4EBiviVa0OfxrdLI64oVK9LWVA/vq/3EYgg96aG9cAv6uAwpjNdSY09TgeLdmToajx/oOgDcYvfFABROnkQezwSxzUStr/Qw=='"
]
},
"metadata": {},
"execution_count": 115
}
]
},
{
"cell_type": "code",
"source": [
"verify_payload(public_pem, license_information)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iIwcSYY8qshk",
"outputId": "5c0d00fb-da26-4a8c-c574-fd018af6b6fc"
},
"execution_count": 116,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'customer_name': 'ABC Company',\n",
" 'pricing_per_seat': '50',\n",
" 'purchased_seat': 50}"
]
},
"metadata": {},
"execution_count": 116
}
]
}
]
}
@0x2b3bfa0
Copy link

0x2b3bfa0 commented Feb 2, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment