aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-01 22:28:43 +0000
committerlloyd <[email protected]>2014-01-01 22:28:43 +0000
commit0c7008498790caea563ed3601df1943f8f7b6269 (patch)
tree2f79b9707e54d2df445881df3148e9b0bca403e7 /doc
parentf5b1caf402ffadadd53b218d14572f8729b2a5c1 (diff)
Move fpe, read_ssh, self_sig, and add X509 print
Diffstat (limited to 'doc')
-rw-r--r--doc/examples/fpe.cpp140
-rw-r--r--doc/examples/read_ssh.cpp130
-rw-r--r--doc/examples/self_sig.cpp72
3 files changed, 0 insertions, 342 deletions
diff --git a/doc/examples/fpe.cpp b/doc/examples/fpe.cpp
deleted file mode 100644
index 8f5eaca9f..000000000
--- a/doc/examples/fpe.cpp
+++ /dev/null
@@ -1,140 +0,0 @@
-#include <botan/botan.h>
-#include <botan/fpe_fe1.h>
-#include <botan/sha160.h>
-
-using namespace Botan;
-
-#include <iostream>
-#include <stdexcept>
-
-namespace {
-
-byte luhn_checksum(u64bit cc_number)
- {
- byte sum = 0;
-
- bool alt = false;
- while(cc_number)
- {
- byte digit = cc_number % 10;
- if(alt)
- {
- digit *= 2;
- if(digit > 9)
- digit -= 9;
- }
-
- sum += digit;
-
- cc_number /= 10;
- alt = !alt;
- }
-
- return (sum % 10);
- }
-
-bool luhn_check(u64bit cc_number)
- {
- return (luhn_checksum(cc_number) == 0);
- }
-
-u64bit cc_rank(u64bit cc_number)
- {
- // Remove Luhn checksum
- return cc_number / 10;
- }
-
-u64bit cc_derank(u64bit cc_number)
- {
- for(u32bit i = 0; i != 10; ++i)
- if(luhn_check(cc_number * 10 + i))
- return (cc_number * 10 + i);
- return 0;
- }
-
-/*
-* Use the SHA-1 hash of the account name or ID as a tweak
-*/
-std::vector<byte> sha1(const std::string& acct_name)
- {
- SHA_160 hash;
- hash.update(acct_name);
- return unlock(hash.final());
- }
-
-u64bit encrypt_cc_number(u64bit cc_number,
- const SymmetricKey& key,
- const std::string& acct_name)
- {
- BigInt n = 1000000000000000;
-
- u64bit cc_ranked = cc_rank(cc_number);
-
- BigInt c = FPE::fe1_encrypt(n, cc_ranked, key, sha1(acct_name));
-
- if(c.bits() > 50)
- throw std::runtime_error("FPE produced a number too large");
-
- u64bit enc_cc = 0;
- for(u32bit i = 0; i != 7; ++i)
- enc_cc = (enc_cc << 8) | c.byte_at(6-i);
- return cc_derank(enc_cc);
- }
-
-u64bit decrypt_cc_number(u64bit enc_cc,
- const SymmetricKey& key,
- const std::string& acct_name)
- {
- BigInt n = 1000000000000000;
-
- u64bit cc_ranked = cc_rank(enc_cc);
-
- BigInt c = FPE::fe1_decrypt(n, cc_ranked, key, sha1(acct_name));
-
- if(c.bits() > 50)
- throw std::runtime_error("FPE produced a number too large");
-
- u64bit dec_cc = 0;
- for(u32bit i = 0; i != 7; ++i)
- dec_cc = (dec_cc << 8) | c.byte_at(6-i);
- return cc_derank(dec_cc);
- }
-
-}
-
-int main(int argc, char* argv[])
- {
- LibraryInitializer init;
-
- if(argc != 4)
- {
- std::cout << "Usage: " << argv[0] << " cc-number acct-name passwd\n";
- return 1;
- }
-
- u64bit cc_number = atoll(argv[1]);
- std::string acct_name = argv[2];
- std::string passwd = argv[3];
-
- std::cout << "Input was: " << cc_number << ' '
- << luhn_check(cc_number) << '\n';
-
- /*
- * In practice something like PBKDF2 with a salt and high iteration
- * count would be a good idea.
- */
- SymmetricKey key(sha1(passwd));
-
- u64bit enc_cc = encrypt_cc_number(cc_number, key, acct_name);
-
- std::cout << "Encrypted: " << enc_cc
- << ' ' << luhn_check(enc_cc) << '\n';
-
- u64bit dec_cc = decrypt_cc_number(enc_cc, key, acct_name);
-
- std::cout << "Decrypted: " << dec_cc
- << ' ' << luhn_check(dec_cc) << '\n';
-
- if(dec_cc != cc_number)
- std::cout << "Something went wrong :( Bad CC checksum?\n";
- }
diff --git a/doc/examples/read_ssh.cpp b/doc/examples/read_ssh.cpp
deleted file mode 100644
index 7ef81b346..000000000
--- a/doc/examples/read_ssh.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
-* (C) 2009 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-/*
-* Example of reading SSH2 format public keys (see RFC 4716)
-*/
-
-#include <botan/x509_key.h>
-#include <botan/filters.h>
-#include <botan/loadstor.h>
-#include <botan/rsa.h>
-#include <botan/dsa.h>
-#include <fstream>
-#include <memory>
-
-using namespace Botan;
-
-namespace {
-
-u32bit read_u32bit(Pipe& pipe)
- {
- byte out[4] = { 0 };
- pipe.read(out, 4);
- u32bit len = load_be<u32bit>(out, 0);
- if(len > 10000)
- throw Decoding_Error("Huge size in read_u32bit, something went wrong");
- return len;
- }
-
-std::string read_string(Pipe& pipe)
- {
- u32bit len = read_u32bit(pipe);
-
- std::string out(len, 'X');
- pipe.read(reinterpret_cast<byte*>(&out[0]), len);
- return out;
- }
-
-BigInt read_bigint(Pipe& pipe)
- {
- u32bit len = read_u32bit(pipe);
-
- secure_vector<byte> buf(len);
- pipe.read(&buf[0], len);
- return BigInt::decode(buf);
- }
-
-Public_Key* read_ssh_pubkey(const std::string& file)
- {
- std::ifstream in(file.c_str());
-
- const std::string ssh_header = "---- BEGIN SSH2 PUBLIC KEY ----";
- const std::string ssh_trailer = "---- END SSH2 PUBLIC KEY ----";
-
- std::string hex_bits;
-
- std::string line;
- std::getline(in, line);
-
- if(line != ssh_header)
- return 0;
-
- while(in.good())
- {
- std::getline(in, line);
-
- if(line.find("Comment: ") == 0)
- {
- while(line[line.size()-1] == '\\')
- std::getline(in, line);
- std::getline(in, line);
- }
-
- if(line == ssh_trailer)
- break;
-
- hex_bits += line;
- }
-
- Pipe pipe(new Base64_Decoder);
- pipe.process_msg(hex_bits);
-
- std::string key_type = read_string(pipe);
-
- if(key_type != "ssh-rsa" && key_type != "ssh-dss")
- return 0;
-
- if(key_type == "ssh-rsa")
- {
- BigInt e = read_bigint(pipe);
- BigInt n = read_bigint(pipe);
- return new RSA_PublicKey(n, e);
- }
- else if(key_type == "ssh-dss")
- {
- BigInt p = read_bigint(pipe);
- BigInt q = read_bigint(pipe);
- BigInt g = read_bigint(pipe);
- BigInt y = read_bigint(pipe);
-
- return new DSA_PublicKey(DL_Group(p, q, g), y);
- }
-
- return 0;
- }
-
-}
-
-#include <botan/init.h>
-#include <iostream>
-
-int main()
- {
- LibraryInitializer init;
-
- std::unique_ptr<Public_Key> key(read_ssh_pubkey("dsa.ssh"));
-
- if(key == 0)
- {
- std::cout << "Failed\n";
- return 1;
- }
-
- std::cout << X509::PEM_encode(*key);
-
- return 0;
- }
diff --git a/doc/examples/self_sig.cpp b/doc/examples/self_sig.cpp
deleted file mode 100644
index 7cb159db9..000000000
--- a/doc/examples/self_sig.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-#include <botan/botan.h>
-#include <botan/x509self.h>
-#include <botan/rsa.h>
-#include <botan/dsa.h>
-using namespace Botan;
-
-#include <iostream>
-#include <fstream>
-#include <memory>
-
-int main(int argc, char* argv[])
- {
- if(argc != 7)
- {
- std::cout << "Usage: " << argv[0]
- << " passphrase [CA|user] name country_code organization email"
- << std::endl;
- return 1;
- }
-
- Botan::LibraryInitializer init;
-
- std::string CA_flag = argv[2];
- bool do_CA = false;
-
- if(CA_flag == "CA") do_CA = true;
- else if(CA_flag == "user") do_CA = false;
- else
- {
- std::cout << "Bad flag for CA/user switch: " << CA_flag << std::endl;
- return 1;
- }
-
- try
- {
- AutoSeeded_RNG rng;
-
- RSA_PrivateKey key(rng, 2048);
- //DL_Group group(rng, DL_Group::DSA_Kosherizer, 2048, 256);
-
- //DSA_PrivateKey key(rng, group);
-
- std::ofstream priv_key("private.pem");
- priv_key << PKCS8::PEM_encode(key, rng, argv[1]);
-
- X509_Cert_Options opts;
-
- opts.common_name = argv[3];
- opts.country = argv[4];
- opts.organization = argv[5];
- opts.email = argv[6];
- /* Fill in other values of opts here */
-
- //opts.xmpp = "[email protected]";
-
- if(do_CA)
- opts.CA_key();
-
- X509_Certificate cert =
- X509::create_self_signed_cert(opts, key, "SHA-256", rng);
-
- std::ofstream cert_file("cert.pem");
- cert_file << cert.PEM_encode();
- }
- catch(std::exception& e)
- {
- std::cout << "Exception: " << e.what() << std::endl;
- return 1;
- }
-
- return 0;
- }