passlib.utils.binary - Binary Helper Functions¶
Internal helpers for binary data.
Warning
This module is primarily used as an internal support module. Its interface has not been finalized yet, and may be changed somewhat between major releases of Passlib, as the internal code is cleaned up and simplified.
Constants¶
BASE64_CHARS¶
Character map used by standard MIME-compatible Base64 encoding scheme.
BASE64_CHARS
module-attribute
¶
BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
HASH64_CHARS¶
Base64 character map used by a number of hash formats; the ordering is wildly different from the standard base64 character map.
This encoding system appears to have originated with passlib.hash.des_crypt, but is used by passlib.hash.md5_crypt, passlib.hash.sha256_crypt, and others. Within Passlib, this encoding is referred as the "hash64" encoding, to distinguish it from normal base64 and others.
HASH64_CHARS
module-attribute
¶
HASH64_CHARS = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
BCRYPT_CHARS¶
Base64 character map used by passlib.hash.bcrypt. The ordering is wildly different from both the standard base64 character map, and the common hash64 character map.
BCRYPT_CHARS
module-attribute
¶
BCRYPT_CHARS = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Base64 Encoding¶
Base64Engine Class¶
Passlib has to deal with a number of different Base64 encodings, with varying endianness, as well as wildly different character <-> value mappings. This is all encapsulated in the Base64Engine class, which provides common encoding actions for an arbitrary base64-style encoding scheme. There are also a couple of predefined instances which are commonly used by the hashes in Passlib.
Base64Engine
¶
Base64Engine(charmap, big=False)
Provides routines for encoding/decoding base64 data using arbitrary character mappings, selectable endianness, etc.
Parameters:
-
charmap–A string of 64 unique characters, which will be used to encode successive 6-bit chunks of data. A character's position within the string should correspond to its 6-bit value.
-
big–Whether the encoding should be big-endian (default False).
.. note:: This class does not currently handle base64's padding characters in any way what so ever.
Raw Bytes <-> Encoded Bytes¶
The following methods convert between raw bytes, and strings encoded using the engine's specific base64 variant:
.. automethod:: encode_bytes .. automethod:: decode_bytes .. automethod:: encode_transposed_bytes .. automethod:: decode_transposed_bytes
.. .. automethod:: check_repair_unused .. automethod:: repair_unused
Integers <-> Encoded Bytes¶
The following methods allow encoding and decoding unsigned integers to and from the engine's specific base64 variant. Endianess is determined by the engine's
bigconstructor keyword... automethod:: encode_int6 .. automethod:: decode_int6
.. automethod:: encode_int12 .. automethod:: decode_int12
.. automethod:: encode_int24 .. automethod:: decode_int24
.. automethod:: encode_int64 .. automethod:: decode_int64
Informational Attributes¶
.. attribute:: charmap
unicode string containing list of characters used in encoding; position in string matches 6bit value of character... attribute:: bytemap
bytes version of :attr:`charmap`.. attribute:: big
boolean flag indicating this using big-endian encoding.
encode_bytes
¶
encode_bytes(source)
encode bytes to base64 string.
Parameters:
-
source–byte string to encode.
Returns:
-
–
byte string containing encoded data.
decode_bytes
¶
decode_bytes(source)
decode bytes from base64 string.
Parameters:
-
source–byte string to decode.
Returns:
-
–
byte string containing decoded data.
check_repair_unused
¶
check_repair_unused(source)
helper to detect & clear invalid unused bits in last character.
Parameters:
-
source–encoded data (as ascii bytes or unicode).
Returns:
-
–
(True, result)if the string was repaired,(False, source)if the string was ok as-is.
encode_transposed_bytes
¶
encode_transposed_bytes(source, offsets)
encode byte string, first transposing source using offset list
decode_transposed_bytes
¶
decode_transposed_bytes(source, offsets)
decode byte string, then reverse transposition described by offset list
decode_int64
¶
decode_int64(source)
decode 11 char base64 string -> 64-bit integer
this format is used primarily by des-crypt & variants to encode the DES output value used as a checksum.
encode_int64
¶
encode_int64(value)
encode 64-bit integer -> 11 char hash64 string
this format is used primarily by des-crypt & variants to encode the DES output value used as a checksum.
Predefined Instances¶
h64¶
Predefined instance of Base64Engine which uses the HASH64_CHARS character map and little-endian encoding. (see HASH64_CHARS above for more details).
h64big¶
Predefined variant of h64 which uses big-endian encoding. This is mainly used by passlib.hash.des_crypt.
Warning
Changed in 1.6: Previous versions of Passlib contained a module named passlib.utils.h64; As of Passlib 1.6 this was replaced by the h64 and h64big instances of the Base64Engine class; the interface remains mostly unchanged.
Other¶
ab64_encode
¶
ab64_encode(data)
encode using shortened base64 format which omits padding & whitespace.
uses custom ./ altchars.
it is primarily used by Passlib's custom pbkdf2 hashes.
ab64_decode
¶
ab64_decode(data)
decode from shortened base64 format which omits padding & whitespace.
uses custom ./ altchars, but supports decoding normal +/ altchars as well.
it is primarily used by Passlib's custom pbkdf2 hashes.
b64s_encode
¶
b64s_encode(data)
encode using shortened base64 format which omits padding & whitespace.
uses default +/ altchars.
b64s_decode
¶
b64s_decode(data)
decode from shortened base64 format which omits padding & whitespace.
uses default +/ altchars.
b32encode
¶
b32encode(source)
wrapper around :func:base64.b32encode which strips padding,
and returns a native string.
b32decode
¶
b32decode(source)
wrapper around :func:base64.b32decode
which handles common mistyped chars.
padding optional, ignored if present.