Skip to main content

A python package that creates and verifies HMAC signatures

Project description

HMAC Authentication (easy-hmac)

A pure python package with no dependencies to easily handle the generation and verification of HMAC signatures.

Installation

This package is hosted at https://pypi.org/project/easy-hmac/

User

If you don't need to customise this package, just install the package from here.

Ie. pip install easy-hmac

Developer

  1. Clone this repository
  2. Install UV
  3. Sync dependencies:
uv sync
  1. Run the tests:
uv run python -m pytest tests/
  1. Build the package:
uv build

Usage

easy-hmac provides two helper functions for HMAC authentication:

  • generate_hmac_sha256 - generates a SHA256 HMAC from two strings (a secret key and a http message)
  • verify_hmac - given an HMAC and a message, verifies if the HMAC generated by the message is equal to the one passed as argument

Step 1

Import the package. python import datetime from easy_hmac import core import hmac import hashlib from base64 import b64encode from typing import Dict, Any

Step 2

Create some vars we can use to generate and verify the HMAC.

# fake identifier used to retrieve the secret from a db.
secret = "79721503-d1ef-46b7-b4ca-fec39ece902f"
body = '{"event": "lifecycle_updated", "payload": {"uuid": "cb8c79cd-8d79-4698-90a2-662eeab8da98", "timestamp": "2021-12-10T00:16:08.048401Z", "status": "PROCESSING"}}'
method = "POST"
timestamp = datetime.datetime.now(datetime.UTC).strftime("%a, %d %b %Y %H:%M:%S GMT")
path = "/api/v1/my/path"

# vars required for verifying the HMAC.
# This step is a little long because we need to fake some parameters we'd usually already have.
identifier = "2e42a19593f047e080285e49864b0fb6"
hash = hashlib.md5(body.encode())
content_type = "application/json"
content_md5 = b64encode(hash.digest()).decode('utf-8')
message = "\n".join([method, content_md5, content_type, timestamp, path])
signature = hmac.new(bytes(secret, "latin-1"), bytes(message, "latin-1"), digestmod=hashlib.sha256)
hmac_base64 = b64encode(signature.digest()).decode("utf-8")

headers = {
    "Date": timestamp,
    "Content-MD5": content_md5,
    "Content-Type": content_type,
    "Authorization": "HMAC {}:{}".format(identifier, hmac_base64),
}
request = {"method": method, "body": body, "path": path, "headers": headers}

Step 3

Create and verify the signature.

result_digest = core.generate_hmac_sha256(secret, method, body, path, timestamp)
actual_signature = b64encode(result_digest).decode()
expected_signature = "d8laojz+oDCPizTL1a401mHq5IpR1A9f9QK3+RQ/6hA="

core.verify_hmac(secret, hmac_base64, headers["Content-MD5"], body.encode(), headers["Date"], headers["Content-Type"], path, method)

You can raise an exception that the request has expired by changing the timestamp to; timestamp = "Fri, 10 Dec 2021 00:16:57 GMT" and then rerunning the second set of vars above to generate an incoming request that is too old and verifying the signature again.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_hmac-1.2.1.tar.gz (98.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_hmac-1.2.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file easy_hmac-1.2.1.tar.gz.

File metadata

  • Download URL: easy_hmac-1.2.1.tar.gz
  • Upload date:
  • Size: 98.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for easy_hmac-1.2.1.tar.gz
Algorithm Hash digest
SHA256 6be2eccab0f75dc911f19232fb9c0299112a3b3ef361033f6b9d9040859e7d64
MD5 09330f517ac5ce72c44fecf744b67ed0
BLAKE2b-256 6492b21afff14cb5830ab76c86714eb0e0d6d67c63facb542684eb960efb5745

See more details on using hashes here.

File details

Details for the file easy_hmac-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: easy_hmac-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for easy_hmac-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7975fd5b2663b6f4eddf909b513c3ba5f08c32e07d9290bb6f9f7be964ecc2f
MD5 1bcffb9151a400b39d6d5615b3c07a4c
BLAKE2b-256 bf623c2862ad0a483ce0ad62b7654bf882c890c7022ae8a0c3e91e1ab939c55e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page