diff options
author | lloyd <[email protected]> | 2009-10-10 20:20:24 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2009-10-10 20:20:24 +0000 |
commit | 5318b944acc2a5fa6d445784c710f37c793ff90b (patch) | |
tree | 94ef9e74fa741f5d841d890dbeb94c5ca5d6678f /doc | |
parent | 9e6720cceee1429658175c92cc8edf101ab4a4b3 (diff) |
Add a couple more Python examples and the very beginning of a manual/reference
for the Python wrappers.
Diffstat (limited to 'doc')
-rw-r--r-- | doc/python.tex | 68 | ||||
-rwxr-xr-x | doc/python/cipher.py | 44 | ||||
-rwxr-xr-x | doc/python/rsa.py | 31 |
3 files changed, 143 insertions, 0 deletions
diff --git a/doc/python.tex b/doc/python.tex new file mode 100644 index 000000000..afdd66b6a --- /dev/null +++ b/doc/python.tex @@ -0,0 +1,68 @@ +\documentclass{article} + +\setlength{\textwidth}{6.5in} % 1 inch side margins +\setlength{\textheight}{9in} % ~1 inch top and bottom margins + +\setlength{\headheight}{0in} +\setlength{\topmargin}{0in} +\setlength{\headsep}{0in} + +\setlength{\oddsidemargin}{0in} +\setlength{\evensidemargin}{0in} + +\title{\textbf{Botan Python Interface Documentation}} +\author{Jack Lloyd \\ + \texttt{[email protected]}} +\date{2009/10/10} + +\newcommand{\filename}[1]{\texttt{#1}} +\newcommand{\manpage}[2]{\texttt{#1}(#2)} + +\newcommand{\macro}[1]{\texttt{#1}} + +\newcommand{\function}[1]{\textbf{#1}} +\newcommand{\type}[1]{\texttt{#1}} +\renewcommand{\arg}[1]{\textsl{#1}} +\newcommand{\variable}[1]{\textsl{#1}} + +\begin{document} + +\maketitle + +\tableofcontents + +\parskip=5pt +\pagebreak + +\section{Ciphers} + +Botan's Python interface provides a generic interface to any cipher +supported by the library. The class \type{botan.Cipher} takes three +arguments, all strings: first, the name of the algorith, second the +direction (which can be either ``encrypt'' or ``decrypt''), and +lastly, the key to use. For instance + +\begin{verbatim} + encryptor = botan.Cipher("AES-128/EAX", "encrypt", key) +\end{verbatim} + +creates an object that will encrypt and authenticate messages using +the EAX mode of operation using the AES cipher. To use this object, +call the \function{cipher} function with two arguments - the input +to encrypt, and the IV to use: + +\begin{verbatim} + ciphertext = encryptor.cipher(input, salt) +\end{verbatim} + + +\subsection{Cryptobox} + + +\subsection{RNGs} + +\section{RSA} + + + +\end{document} diff --git a/doc/python/cipher.py b/doc/python/cipher.py new file mode 100755 index 000000000..1be2759ae --- /dev/null +++ b/doc/python/cipher.py @@ -0,0 +1,44 @@ +#!/usr/bin/python + +import botan +import sys + +def encrypt(input, passphrase): + rng = botan.RandomNumberGenerator() + + # Use as both EAX IV and PBKDF2 salt + salt = rng.gen_random(10) + + iterations = 10000 + output_size = 16 + + key = botan.pbkdf2(passphrase, salt, iterations, output_size, "SHA-1") + + encryptor = botan.Cipher("AES-128/EAX", "encrypt", key) + + ciphertext = encryptor.cipher(input, salt) + return (ciphertext, salt) + +def decrypt(input, salt, passphrase): + iterations = 10000 + output_size = 16 + + key = botan.pbkdf2(passphrase, salt, iterations, output_size, "SHA-1") + + decryptor = botan.Cipher("AES-128/EAX", "decrypt", key) + + return decryptor.cipher(input, salt) + +def main(args = None): + if args is None: + args = sys.argv + + passphrase = args[1] + input = ''.join(open(args[2]).readlines()) + + (ciphertext, salt) = encrypt(input, passphrase) + + print decrypt(ciphertext, salt, passphrase) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/doc/python/rsa.py b/doc/python/rsa.py new file mode 100755 index 000000000..15ffcffa3 --- /dev/null +++ b/doc/python/rsa.py @@ -0,0 +1,31 @@ +#!/usr/bin/python + +import botan + +rng = botan.RandomNumberGenerator() + +rsa_priv = botan.RSA_PrivateKey(768, rng) + +print rsa_priv.to_string() +print int(rsa_priv.get_N()) +print int(rsa_priv.get_E()) + + +rsa_pub = botan.RSA_PublicKey(rsa_priv) + +key = rng.gen_random(20) + +ciphertext = rsa_pub.encrypt(key, 'EME1(SHA-1)', rng) + +print ciphertext.encode('hex') + +plaintext = rsa_priv.decrypt(ciphertext, 'EME1(SHA-1)') + +print plaintext == key + + +signature = rsa_priv.sign(key, 'EMSA4(SHA-256)', rng) + +key = key.replace('a', 'b') + +print rsa_pub.verify(key, signature, 'EMSA4(SHA-256)') |