Utilities

tomcrypt.utils.bytes_equal()

Constant-time byte sequence equality.

Good for removing a huge potential timing attack.

Parameters:
  • a (bytes) – One sequence of bytes.
  • b (bytes) – Another sequence of bytes.
Return bool:

True if the sequences are the same.

Raises ValueError:
 

When the byte sequences have different lengths.

>>> bytes_equal(b'hello', b'hello')
True
>>> bytes_equal(b'hello', b'world')
False
tomcrypt.utils.pem_decode()

PEM decode a key.

Returns a tuple of:

  • type: E.g. "RSA", "EC", or None (likely if public);
  • mode: "PRIVATE" or "PUBLIC";
  • content: original content.

Throws a tomcrypt.Error if content is not PEM encoded.

tomcrypt.utils.pem_encode()

PEM encode a key.

Parameters:
  • type (str) – "RSA", "EC", etc.
  • mode (str) – "PUBLIC" or "PRIVATE"
  • content (bytes) – The content to encode.
>>> print(pem_encode('TEST', 'PRIVATE', b'private content'))
-----BEGIN TEST PRIVATE KEY-----
cHJpdmF0ZSBjb250ZW50
-----END TEST PRIVATE KEY-----
>>> print(pem_encode('TEST', 'PUBLIC', b'public content'))
-----BEGIN PUBLIC KEY-----
cHVibGljIGNvbnRlbnQ=
-----END PUBLIC KEY-----
tomcrypt.utils.xor_bytes()

XOR two sequences of bytes.

Parameters:
  • a (bytes) – One sequence of bytes.
  • b (bytes) – Another sequence of bytes.
Return bytes:

A sequence of bytes, the same length of a and b in which each byte is the XOR of the respective bytes from a and b.

Raises ValueError:
 

When the byte sequences have different lengths.

>>> xor_bytes(b'hello', b'world')
b'\x1f\n\x1e\x00\x0b'
>>> xor_bytes(b'hello', b'hello')
b'\x00\x00\x00\x00\x00'

Previous topic

PKCS5

This Page