Python Binding

New in version 1.11.14.

The Python binding is based on the ffi module of botan and the ctypes module of the Python standard library.

Starting in 2.8, the class names were renamed to match Python standard conventions. However aliases are defined which allow older code to continue to work; the older names are mentioned as “previously X”.



Returns the major number of the library version.


Returns the minor number of the library version.


Returns the patch number of the library version.


Returns a free form version string for the library

Random Number Generators

class botan.RandomNumberGenerator(rng_type = 'system')

Previously rng

Type ‘user’ also allowed (userspace HKDF RNG seeded from system rng). The system RNG is very cheap to create, as just a single file handle or CSP handle is kept open, from first use until shutdown, no matter how many ‘system’ rng instances are created. Thus it is easy to use the RNG in a one-off way, with botan.rng().get(32).


Return some bits

reseed(bits = 256)

Meaningless on system RNG, on userspace RNG causes a reseed/rekey

reseed_from_rng(source_rng, bits = 256)

Take bits from the source RNG and use it to seed self


Add some unpredictable seed data to the RNG

Hash Functions

class botan.HashFunction(algo)

Previously hash_function

Algo is a string (eg ‘SHA-1’, ‘SHA-384’, ‘Skein-512’)


Returns the name of this algorithm


Clear state


Return output length in bytes


Add some input


Returns the hash of all input provided, resets for another message.

Message Authentication Codes

class botan.MsgAuthCode(algo)

Previously message_authentication_code

Algo is a string (eg ‘HMAC(SHA-256)’, ‘Poly1305’, ‘CMAC(AES-256)’)


Returns the name of this algorithm


Clear internal state including the key


Return the output length in bytes


Set the key


Add some input


Returns the MAC of all input provided, resets for another message with the same key.


class botan.SymmetricCipher(object, algo, encrypt = True)

Previously cipher

The algorithm is spcified as a string (eg ‘AES-128/GCM’, ‘Serpent/OCB(12)’, ‘Threefish-512/EAX’).

Set the second param to False for decryption


Returns the name of this algorithm


Returns the tag length (0 for unauthenticated modes)


Returns default nonce length


Returns update block size. Call to update() must provide input of exactly this many bytes


Returns True if this is an AEAD mode


Returns True if nonce_len is a valid nonce len for this mode


Resets all state


Set the key


Sets the associated data. Fails if this is not an AEAD mode


Start processing a message using nonce


Consumes input text and returns output. Input text must be of update_granularity() length. Alternately, always call finish with the entire message, avoiding calls to update entirely

finish(txt = None)

Finish processing (with an optional final input). May throw if message authentication checks fail, in which case all plaintext previously processed must be discarded. You may call finish() with the entire message


botan.bcrypt(passwd, rng, work_factor = 10)

Provided the password and an RNG object, returns a bcrypt string

botan.check_bcrypt(passwd, bcrypt)

Check a bcrypt hash against the provided password, returning True iff the password matches.


botan.pbkdf(algo, password, out_len, iterations = 100000, salt = None)

Runs a PBKDF2 algo specified as a string (eg ‘PBKDF2(SHA-256)’, ‘PBKDF2(CMAC(Blowfish))’). Runs with specified iterations, with meaning depending on the algorithm. The salt can be provided or otherwise is randomly chosen. In any case it is returned from the call.

Returns out_len bytes of output (or potentially less depending on the algorithm and the size of the request).

Returns tuple of salt, iterations, and psk

botan.pbkdf_timed(algo, password, out_len, ms_to_run = 300, salt = rng().get(12))

Runs for as many iterations as needed to consumed ms_to_run milliseconds on whatever we’re running on. Returns tuple of salt, iterations, and psk


New in version 2.8.0.

botan.scrypt(out_len, password, salt, N=1024, r=8, p=8)

Runs Scrypt key derivation function over the specified password and salt using Scrypt parameters N, r, p.


botan.kdf(algo, secret, out_len, salt)

Performs a key derviation function (such as “HKDF(SHA-384)”) over the provided secret and salt values. Returns a value of the specified length.

Public Key

class botan.PublicKey(object)

Previously public_key

fingerprint(hash = 'SHA-256')

Returns a hash of the public key


Returns the algorithm name


Returns the estimated strength of this key against known attacks (NFS, Pollard’s rho, etc)


Returns the encoding of the key, PEM if set otherwise DER

Private Key

class botan.PrivateKey(algo, param, rng)

Previously private_key

Constructor creates a new private key. The parameter type/value depends on the algorithm. For “rsa” is is the size of the key in bits. For “ecdsa” and “ecdh” it is a group name (for instance “secp256r1”). For “ecdh” there is also a special case for group “curve25519” (which is actually a completely distinct key type with a non-standard encoding).


Return a public_key object


Public Key Operations

class botan.PKEncrypt(pubkey, padding)

Previously pk_op_encrypt

encrypt(msg, rng)
class botan.PKDecrypt(privkey, padding)

Previously pk_op_decrypt

class botan.PKSign(privkey, hash_w_padding)

Previously pk_op_sign

class botan.PKVerify(pubkey, hash_w_padding)

Previously pk_op_verify

class botan.PKKeyAgreement(privkey, kdf)

Previously pk_op_key_agreement


Returns the public value to be passed to the other party

agree(other, key_len, salt)

Returns a key derived by the KDF.

Multiple Precision Integers (MPI)

New in version 2.8.0.

class botan.MPI(initial_value=None)

Initialize an MPI object with specified value, left as zero otherwise. The initial_value should be an int, str, or MPI.

Most of the usual arithmetic operators (__add__, __mul__, etc) are defined.


Return the inverse of self modulo modulus, or zero if no inverse exists

is_prime(rng, prob=128)

Test if self is prime

pow_mod(exponent, modulus):

Return self to the exponent power modulo modulus

Format Preserving Encryption (FE1 scheme)

New in version 2.8.0.

class botan.FormatPreservingEncryptionFE1(modulus, key, rounds=5, compat_mode=False)

Initialize an instance for format preserving encryption

encrypt(msg, tweak)

The msg should be a botan2.MPI or an object which can be converted to one

decrypt(msg, tweak)

The msg should be a botan2.MPI or an object which can be converted to one


New in version 2.8.0.

class botan.HOTP(key, hash="SHA-1", digits=6)

Generate an HOTP code for the provided counter

check(code, counter, resync_range=0)

Check if provided code is the correct code for counter. If resync_range is greater than zero, HOTP also checks up to resync_range following counter values.

Returns a tuple of (bool,int) where the boolean indicates if the code was valid, and the int indicates the next counter value that should be used. If the code did not verify, the next counter value is always identical to the counter that was passed in. If the code did verify and resync_range was zero, then the next counter will always be counter+1.