🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

hmac2

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hmac2

A small test package for hmac2.

pipPyPI
Version
0.0.1
Maintainers
1

HMAC2 Documentation

Overview

The HMAC class implements an HMAC (Hash-based Message Authentication Code) using a specified hash function. It provides a simple interface to generate HMAC values using a key and a hash algorithm like SHA256, MD5, etc.

Usage

Creating an HMAC Instance

from hmac2 import HMAC

# Define a key and a message
key = b'secret_key'
message = b'my message'

# Create a new HMAC instance with the key, message, and hash algorithm
h = HMAC(key, message, digestmod='sha256')

Updating with More Data

If you need to add more data to the existing HMAC computation:

h.update(b' additional message')

Getting the Final HMAC Value

# Get the HMAC value in hexadecimal format
print(h.hexdigest())

# Alternatively, get the raw binary value
print(h.digest())

Methods

  • __init__(self, key, msg=None, digestmod='sha256')
    Initializes an HMAC instance with a given key, an optional message, and a digest algorithm. The default digest algorithm is 'sha256'.

  • update(self, msg)
    Updates the HMAC object with more message bytes. You can call this method multiple times with different parts of the message.

  • digest(self)
    Returns the raw HMAC value as a byte sequence.

  • hexdigest(self)
    Returns the HMAC value as a hexadecimal string.

Alternative Usage with new Function

You can also use the new function for a more concise syntax:

import hmac2

# Define a key and a message
key = b'secret_key'
message = b'my message'

# Create a new HMAC and get the result in hex format
hmac_value = hmac2.new(key, message, 'sha256').hexdigest()
print("HMAC: ", hmac_value)

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts