Crypto-related python routines, for exposition, not performance
Dependencies: from the msmath package: bitstring.py, rational.py, conversions.py, numfuns.py
rc4.py contains code for the RC4 one-time pad generator
rsa.py contains code for creating and using RSA key pairs
md5.py contains code for the deprecated MD5 hash algorithm described in RFC1321
sha2.py contains code for algorithms described in NIST.FIPS.180-4, including SHA1 and SHA2 family
sha3.py contains code for algorithms described in NIST.FIPS.202, including SHAKE and SHA3 family
shatest.py runs SHA1, SHA2, and SHA3 with various test vectors and prints the results
Use python help for more current and complete documentation.
The bitstring constructor can create a bitstring instance from a bitstring (a copy)
with the bits reversed if the optional second arg is nonzero,
a string (converting each character c to ord(c) and assuming it to be 8 bits long)
with the octets reversed if the optional second arg is nonzero,
a bytes instance with an optional second arg to reverse the octet order,
or an integer (with big-endian bit numbering) together with a number of bits
which if negative uses little-endian bit numbering (i.e., reverses the bit order).
Examples:
bitstring('abc') -> 011000010110001001100011 [printed as 0x616263]
bitstring(0x37,7) -> 0110111 [printed as 0x37]
bitstring instances have the following attributes:
.x | the bitstring interpreted as a big-endian integer |
._l | the number of bits in the bitstring (same as len()) |
In the bitstring class, the following operators are defined:
len() | the length of the bitstring, in bits |
- | negation mod 2**length |
~ | bitwise complement |
+ | addition mod 2**length |
- | subtraction mod 2**length |
^ | bitwise exclusive or |
& | bitwise and |
| | bitwise or |
<<k | rotate left k bit positions [not shift!] |
>>k | rotate right k bit positions [not shift!] |
*k | concatenate k copies |
int() | the bitstring as a big-endian number |
[sequence of indices and slices] | the bitstring of the concatenated selected bits |
[sequence of indices and slices] = bitstring or integer | the selected bits are set |
In the bitstring class, the following functions are defined:
.concat(sequence of bitstrings and 0s and 1s) | returns the bitstring resulting from concatenating the bitstring with the sequence of bits and bitstrings |
.tacnoc(sequence of bitstrings and 0s and 1s) | returns the bitstring resulting from concatenating the reversed sequence of bits and bitstrings with the bitstring |
.trunc(n) | equivalent to [:n] if 0 <= n <= ._l, else if n >= 0, concatenate 0s so resulting length is always exactly n |
if n < 0, truncate from the left or concatenate 0s on the left so that the resulting length is always exactly -n |
sha3.py also implements the following function:
b3x() | takes a bitstring and produces a new bitstring by reversing each sequence of 8 bits, with any remaining bits also reversed; this can be used to compensate for the little-endian specification of SHA3 (see shatest.py) |
sha3.py also implements the plane class