Browse Source

bring in encode functions from layer7

git-svn-id: file:///home/pjones/src/tmp/b2-svn/cxxtools/trunk@190 637e2b85-2e0a-0410-84ac-bd785500051d
master
pjones 13 years ago
parent
commit
7ef345a720
2 changed files with 231 additions and 0 deletions
  1. 87
    0
      include/cxxtools/encode.h
  2. 144
    0
      src/encode.cxx

+ 87
- 0
include/cxxtools/encode.h View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2002-2006 Peter J Jones (pjones@pmade.org)
* All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef _cxxtools_encode_h_
#define _cxxtools_encode_h_

// standard includes
#include <string>

namespace Cxxtools {
namespace Encode {

//####################################################################
/**
* Base64 encode the given string.
*
* @param s The string to encode.
* @return The encoded string.
* @author Peter Jones
**/
//####################################################################
std::string base64_encode (const std::string &s);

//####################################################################
/**
* Decode the given Base64 string.
*
* @param s The string to decode.
* @return The decoded string.
* @author Peter Jones
**/
//####################################################################
std::string base64_decode (const std::string &s);

//####################################################################
/**
* Digest the given string using the MD5 algorithm.
*
* @param s The string to digest.
* @return The result of digesting s with MD5.
* @author Peter Jones
**/
//####################################################################
std::string md5 (const std::string &s);

//####################################################################
/**
* Digest the given string using the SHA1 algorithm.
*
* @param s The string to digest.
* @return The result of digesting s with SHA1.
* @author Peter Jones
**/
//####################################################################
std::string sha1 (const std::string &s);

}} // end Cxxtools and Encode namespaces
#endif

+ 144
- 0
src/encode.cxx View File

@@ -0,0 +1,144 @@
/*
* Copyright (C) 2002-2006 Peter J Jones (pjones@pmade.org)
* All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

// Cxxtools includes
#include "cxxtools/encode.h"

// OpenSSL includes
#include <openssl/buffer.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/md5.h>
#include <openssl/sha.h>

// standard includes
#include <new>

//####################################################################
namespace
{
const char hex[] = "0123456789abcdef";

struct auto_bio {
auto_bio (BIO *b) : bio(b)
{ if (!bio) throw std::bad_alloc(); }

~auto_bio (void)
{ if (bio) BIO_free(bio); }

void release (void)
{ bio = 0; }

BIO *bio;
};

void bin2hex (const unsigned char *hash, unsigned char *buf, std::size_t len);
}
//####################################################################
std::string Cxxtools::Encode::base64_encode (const std::string &s)
{
auto_bio b64(BIO_new(BIO_f_base64()));
auto_bio mem(BIO_new(BIO_s_mem()));
BIO *bio = BIO_push(b64.bio, mem.bio);
BIO_set_flags(b64.bio, BIO_FLAGS_BASE64_NO_NL);

if (BIO_write(bio, s.c_str(), s.size()) <= 0) return std::string();
BIO_flush(bio);

BUF_MEM *bptr;
std::string out;

BIO_get_mem_ptr(bio, &bptr);
out.assign(bptr->data, bptr->length);

return out;
}
//####################################################################
std::string Cxxtools::Encode::base64_decode (const std::string &s)
{
/*
* We have to do a const_cast in this function because the OpenSSL
* interface wants a void* even though it is read-only.
*/

auto_bio b64(BIO_new(BIO_f_base64()));
auto_bio mem(BIO_new_mem_buf(const_cast<char*>(s.c_str()), s.size()));
BIO *bio = BIO_push(b64.bio, mem.bio);
BIO_set_flags(b64.bio, BIO_FLAGS_BASE64_NO_NL);

char buf[256];
int len;
std::string out;
while ( (len = BIO_read(bio, buf, sizeof(buf))) > 0)
out.append(buf, len);

return out;
}
//####################################################################
std::string Cxxtools::Encode::md5 (const std::string &s)
{
unsigned char buf1[MD5_DIGEST_LENGTH];
unsigned char buf2[MD5_DIGEST_LENGTH * 2 + 1];

MD5(reinterpret_cast<const unsigned char*>(s.c_str()), s.size(), buf1);
bin2hex(buf1, buf2, sizeof(buf1));

return std::string(reinterpret_cast<const char*>(buf2));
}
//####################################################################
std::string Cxxtools::Encode::sha1 (const std::string &s)
{
unsigned char buf1[SHA_DIGEST_LENGTH];
unsigned char buf2[SHA_DIGEST_LENGTH * 2 + 1];

SHA1(reinterpret_cast<const unsigned char*>(s.c_str()), s.size(), buf1);
bin2hex(buf1, buf2, sizeof(buf1));

return std::string(reinterpret_cast<const char*>(buf2));
}
//####################################################################
namespace
{
//####################################################################
void bin2hex (const unsigned char *hash, unsigned char *buf, std::size_t len)
{
for (std::size_t i=0; i!=len; ++i) {
buf[i+i] = hex[hash[i] >> 4];
buf[i+i+1] = hex[hash[i] & 0x0f];
}

buf[len * 2] = 0;
}
//####################################################################
}
//####################################################################

Loading…
Cancel
Save