uhashlib – hashing algorithm

The uhashlib module provides hashing and checksum functions, including both standard cryptographic hashes and game-specific checksum algorithms. All functions return byte arrays containing the hash/checksum result.

Constants

uhashlib.CRC_16_BITS = 16

Constant representing 16-bit CRC width for custom CRC calculations.

uhashlib.CRC_32_BITS = 32

Constant representing 32-bit CRC width for custom CRC calculations.

uhashlib.CRC_64_BITS = 64

Constant representing 64-bit CRC width for custom CRC calculations.

Standard Cryptographic Hash Functions

These functions implement standard cryptographic hash algorithms.

uhashlib.md5(data)

Computes the MD5 hash (128-bit) of the input data.

Parameters:

data (bytes) – Input data to hash

Returns:

16-byte MD5 hash

Return type:

bytes

uhashlib.sha1(data)

Computes the SHA-1 hash (160-bit) of the input data.

Parameters:

data (bytes) – Input data to hash

Returns:

20-byte SHA-1 hash

Return type:

bytes

uhashlib.sha224(data)

Computes the SHA-224 hash (224-bit, truncated SHA-256) of the input data.

Parameters:

data (bytes) – Input data to hash

Returns:

28-byte SHA-224 hash

Return type:

bytes

uhashlib.sha256(data)

Computes the SHA-256 hash (256-bit) of the input data.

Parameters:

data (bytes) – Input data to hash

Returns:

32-byte SHA-256 hash

Return type:

bytes

uhashlib.sha384(data)

Computes the SHA-384 hash (384-bit, truncated SHA-512) of the input data.

Parameters:

data (bytes) – Input data to hash

Returns:

48-byte SHA-384 hash

Return type:

bytes

uhashlib.sha512(data)

Computes the SHA-512 hash (512-bit) of the input data.

Parameters:

data (bytes) – Input data to hash

Returns:

64-byte SHA-512 hash

Return type:

bytes

uhashlib.hmac_sha1(key, data)

Computes HMAC-SHA1 (Hash-based Message Authentication Code using SHA-1).

Parameters:
  • key (bytes) – HMAC key

  • data (bytes) – Input data to authenticate

Returns:

20-byte HMAC-SHA1 result

Return type:

bytes

Password-Based Key Derivation Functions (PBKDF2)

The PBKDF2 (Password-Based Key Derivation Function 2) functions implement key derivation from passwords using HMAC with SHA-1 or SHA-256. These functions are commonly used for password hashing and key derivation in cryptographic applications.

uhashlib.pbkdf2_sha1(password, salt, iterations, dklen)

Derives a cryptographic key from a password using PBKDF2 with HMAC-SHA1.

PBKDF2 applies a pseudorandom function (HMAC-SHA1) to the input password along with a salt value and repeats the process many times to produce a derived key. This “key stretching” technique makes brute-force attacks more difficult.

Parameters:
  • password (bytes) – Password or passphrase from which to derive the key

  • salt (bytes) – Cryptographic salt (should be random and unique for each password)

  • iterations (int) – Number of iterations (higher values increase security but slow down computation)

  • dklen (int) – Desired length of the derived key in bytes

Returns:

Derived key as bytes

Return type:

bytes

Security Considerations:

  • Use a cryptographically random salt of at least 16 bytes

  • Use a high iteration count (typically 100,000 to 1,000,000)

  • SHA-1 is considered cryptographically weak; prefer pbkdf2_sha256 when possible

  • The derived key length should match the requirements of the cryptographic algorithm using it

Example:

import uhashlib
import os

password = b"mySecretPassword123"
salt = os.urandom(16)  # Generate random salt
derived_key = uhashlib.pbkdf2_sha1(password, salt, 100000, 32)
# Returns 32-byte derived key
uhashlib.pbkdf2_sha256(password, salt, iterations, dklen)

Derives a cryptographic key from a password using PBKDF2 with HMAC-SHA256.

This is a more secure variant using SHA-256 as the underlying hash function. It provides better cryptographic security than SHA-1 and is recommended for new applications.

Parameters:
  • password (bytes) – Password or passphrase from which to derive the key

  • salt (bytes) – Cryptographic salt (should be random and unique for each password)

  • iterations (int) – Number of iterations (higher values increase security but slow down computation)

  • dklen (int) – Desired length of the derived key in bytes

Returns:

Derived key as bytes

Return type:

bytes

Security Considerations:

  • Use a cryptographically random salt of at least 16 bytes

  • Use a high iteration count (typically 100,000 to 1,000,000)

  • SHA-256 is currently considered cryptographically secure

  • Consider using even higher iteration counts for very sensitive applications

Example:

import uhashlib
import os

password = b"mySecretPassword123"
salt = os.urandom(16)  # Generate random salt
derived_key = uhashlib.pbkdf2_sha256(password, salt, 100000, 32)
# Returns 32-byte derived key

Standard Checksum Functions

These functions implement common checksum and non-cryptographic hash algorithms.

uhashlib.adler16(data)

Computes Adler-16 checksum (16-bit) of the input data.

Parameters:

data (bytes) – Input data to checksum

Returns:

2-byte Adler-16 checksum (big-endian)

Return type:

bytes

uhashlib.adler32(data, init=0)

Computes Adler-32 checksum (32-bit) of the input data.

Parameters:
  • data (bytes) – Input data to checksum

  • init (int) – Optional initial value (default: 0)

Returns:

4-byte Adler-32 checksum (big-endian)

Return type:

bytes

uhashlib.crc16(data)

Computes standard CRC-16 (CRC-16-IBM) checksum.

Parameters:

data (bytes) – Input data to checksum

Returns:

2-byte CRC-16 checksum (big-endian)

Return type:

bytes

uhashlib.crc32(data)

Computes standard CRC-32 (CRC-32/ISO-HDLC) checksum with reflected input/output.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte CRC-32 checksum (big-endian)

Return type:

bytes

uhashlib.crc32big(data)

Computes CRC-32/BZIP checksum without reflection (big-endian).

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte CRC-32 checksum (big-endian)

Return type:

bytes

uhashlib.crc64_ecma(data)

Computes CRC-64-ECMA checksum.

Parameters:

data (bytes) – Input data to checksum

Returns:

8-byte CRC-64-ECMA checksum (big-endian)

Return type:

bytes

uhashlib.crc64_iso(data)

Computes CRC-64-ISO checksum.

Parameters:

data (bytes) – Input data to checksum

Returns:

8-byte CRC-64-ISO checksum (big-endian)

Return type:

bytes

uhashlib.crc(data, width, poly, init, xor, refIn, refOut)

Computes custom CRC with user-defined parameters.

Parameters:
  • data (bytes) – Input data to checksum

  • width (int) – CRC width in bits (16, 32, or 64)

  • poly (int) – Polynomial value

  • init (int) – Initial value

  • xor (int) – Final XOR value

  • refIn (int) – Input reflection (0=no, 1=yes)

  • refOut (int) – Output reflection (0=no, 1=yes)

Returns:

CRC result (2, 4, or 8 bytes depending on width)

Return type:

bytes

Raises:

ValueError – If width is not 16, 32, or 64

uhashlib.force_crc32(data, offset, newcrc)

Forces a specific CRC-32 value at a given offset in the data.

Parameters:
  • data (bytes) – Input data (modified to have target CRC)

  • offset (int) – Byte offset where CRC should be placed

  • newcrc (int) – Desired CRC-32 value

Returns:

4-byte modified CRC value

Return type:

bytes

Game-Specific Checksum Functions

These functions implement checksum algorithms used by specific video games.

uhashlib.ea_checksum(data)

Computes checksum used by Electronic Arts games (MC02 algorithm).

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte EA checksum (big-endian)

Return type:

bytes

uhashlib.ffx_checksum(data)

Computes checksum used by Final Fantasy X.

Parameters:

data (bytes) – Input data to checksum

Returns:

2-byte FFX checksum (little-endian)

Return type:

bytes

uhashlib.ff13_checksum(data)

Computes checksum used by Final Fantasy XIII.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte FFXIII checksum (little-endian)

Return type:

bytes

uhashlib.kh25_checksum(data)

Computes checksum used by Kingdom Hearts 2.5.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte KH2.5 checksum (little-endian)

Return type:

bytes

uhashlib.khcom_checksum(data)

Computes checksum used by Kingdom Hearts: Chain of Memories.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte KH:CoM checksum (big-endian)

Return type:

bytes

uhashlib.mgs2_checksum(data)

Computes checksum used by Metal Gear Solid 2.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte MGS2 checksum (big-endian)

Return type:

bytes

uhashlib.mgspw_checksum(data)

Computes checksum used by Metal Gear Solid: Peace Walker.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte MGSPW checksum (big-endian)

Return type:

bytes

uhashlib.sw4_checksum(data)

Computes 4-part checksum used by Samurai Warriors 4 (Sengoku Musou 4).

Parameters:

data (bytes) – Input data to checksum

Returns:

Tuple of 4 4-byte checksums (big-endian)

Return type:

tuple

uhashlib.toz_checksum(data)

Computes 20-byte checksum used by Tales of Zestiria.

Parameters:

data (bytes) – Input data to checksum

Returns:

20-byte ToZ checksum

Return type:

bytes

uhashlib.tiara2_checksum(data)

Computes checksum used by Tears to Tiara 2 games.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte Tiara 2 checksum (big-endian)

Return type:

bytes

uhashlib.castlevania_checksum(data)

Computes checksum used by Castlevania: Lords of Shadow 1 & 2.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte Castlevania checksum (little-endian)

Return type:

bytes

uhashlib.rockstar_checksum(data)

Computes and updates Rockstar Games CHKS checksums in data.

Parameters:

data (bytes) – Input data containing CHKS blocks

Returns:

4-byte final CHKS value (big-endian)

Return type:

bytes

uhashlib.dbzxv2_checksum(data)

Computes checksum used by Dragon Ball Z: Xenoverse 2.

Parameters:

data (bytes) – Input data to checksum

Returns:

8-byte DBZXV2 checksum (big-endian)

Return type:

bytes

uhashlib.deadrising_checksum(data)

Computes checksum used by Dead Rising games.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte Dead Rising checksum (big-endian)

Return type:

bytes

Custom Hash Functions

These functions implement various custom or modified hash algorithms.

uhashlib.md5_xor(data)

Computes MD5 hash and XORs the four 32-bit words together.

Parameters:

data (bytes) – Input data to hash

Returns:

4-byte XOR result of MD5 words (big-endian)

Return type:

bytes

uhashlib.sha1_xor64(data)

Computes SHA-1 hash and XORs to produce a 64-bit value.

Parameters:

data (bytes) – Input data to hash

Returns:

8-byte XOR result of SHA-1 (big-endian)

Return type:

bytes

uhashlib.checksum32(data)

Computes a simple 32-bit checksum.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte checksum (big-endian)

Return type:

bytes

uhashlib.add(data, carry=0)

Computes additive checksum (sum of bytes).

Parameters:
  • data (bytes) – Input data to checksum

  • carry (int) – Carry value (must be 2 if provided)

Returns:

2 or 4-byte additive checksum (big-endian)

Return type:

bytes

Raises:

ValueError – If carry is specified and not 2

uhashlib.wadd(data, carry=0)

Computes word-additive checksum (sum of 16-bit words, big-endian).

Parameters:
  • data (bytes) – Input data to checksum

  • carry (int) – Carry value (must be 2 if provided)

Returns:

2 or 4-byte word-additive checksum (big-endian)

Return type:

bytes

Raises:

ValueError – If carry is specified and not 2

uhashlib.wadd_le(data)

Computes word-additive checksum (sum of 16-bit words, little-endian).

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte word-additive checksum (big-endian)

Return type:

bytes

uhashlib.dwadd(data)

Computes double-word additive checksum (sum of 32-bit words, big-endian).

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte double-word additive checksum (big-endian)

Return type:

bytes

uhashlib.dwadd_le(data)

Computes double-word additive checksum (sum of 32-bit words, little-endian).

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte double-word additive checksum (big-endian)

Return type:

bytes

uhashlib.qwadd(data)

Computes quad-word additive checksum (sum of 64-bit words).

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte quad-word additive checksum (big-endian)

Return type:

bytes

uhashlib.wsub(data)

Computes word-subtractive checksum.

Parameters:

data (bytes) – Input data to checksum

Returns:

4-byte word-subtractive checksum (big-endian)

Return type:

bytes

General-Purpose Hash Functions

These functions implement various general-purpose non-cryptographic hash algorithms.

uhashlib.sdbm(data, init=0)

Computes SDBM hash.

Parameters:
  • data (bytes) – Input data to hash

  • init (int) – Optional initial value (default: 0)

Returns:

4-byte SDBM hash (big-endian)

Return type:

bytes

uhashlib.fnv1(data, init=FNV1_INIT_VALUE)

Computes FNV-1 (Fowler-Noll-Vo) hash.

Parameters:
  • data (bytes) – Input data to hash

  • init (int) – Optional initial value (default: 0x811c9dc5)

Returns:

4-byte FNV-1 hash (big-endian)

Return type:

bytes

uhashlib.djb2(data)

Computes DJB2 hash (Daniel J. Bernstein hash).

Parameters:

data (bytes) – Input data to hash

Returns:

4-byte DJB2 hash (big-endian)

Return type:

bytes

uhashlib.murmur3_32(data, init=0)

Computes MurmurHash3 32-bit hash.

Parameters:
  • data (bytes) – Input data to hash

  • init (int) – Optional initial value (default: 0)

Returns:

4-byte MurmurHash3 hash (big-endian)

Return type:

bytes

uhashlib.jhash(data, init=0)

Computes jhash (Jenkins hash).

Parameters:
  • data (bytes) – Input data to hash

  • init (int) – Optional initial value (default: 0)

Returns:

4-byte jhash (big-endian)

Return type:

bytes

uhashlib.jenkins_oaat(data, init=0)

Computes Jenkins one-at-a-time hash.

Parameters:
  • data (bytes) – Input data to hash

  • init (int) – Optional initial value (default: 0)

Returns:

4-byte Jenkins OAAT hash (big-endian)

Return type:

bytes

uhashlib.lookup3_little2(data, iv1, iv2)

Computes lookup3 little-endian hash producing two 32-bit values.

Parameters:
  • data (bytes) – Input data to hash

  • iv1 (int) – First initialization vector

  • iv2 (int) – Second initialization vector

Returns:

Tuple of two 4-byte hash values (big-endian)

Return type:

tuple

Usage Notes

  • All functions return the hash/checksum as a bytes object

  • Most checksums are returned in big-endian format unless otherwise specified

  • For functions with optional init parameters, these provide initial hash values

  • The rockstar_checksum function modifies CHKS blocks in-place within the input data

  • Input data should be provided as bytes or bytearray objects

Example

import uhashlib

# Compute standard hashes
data = b"Hello World"
md5_hash = uhashlib.md5(data)
sha256_hash = uhashlib.sha256(data)

# Compute game-specific checksums
save_data = b"..."
ffx_checksum = uhashlib.ffx_checksum(save_data)
mgs2_checksum = uhashlib.mgs2_checksum(save_data)

# Compute custom CRC
crc_result = uhashlib.crc(
    width=uhashlib.CRC_32_BITS,
    data=data,
    poly=0x04C11DB7,
    init=0xFFFFFFFF,
    xor=0xFFFFFFFF,
    refIn=1,
    refOut=1
)