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-bd78550005
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 @@
1
+/*
2
+ * Copyright (C) 2002-2006 Peter J Jones (pjones@pmade.org)
3
+ * All Rights Reserved
4
+ * 
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ * 
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ *    notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ *    notice, this list of conditions and the following disclaimer in
13
+ *    the documentation and/or other materials provided with the
14
+ *    distribution.
15
+ * 3. Neither the name of the Author nor the names of its contributors
16
+ *    may be used to endorse or promote products derived from this software
17
+ *    without specific prior written permission.
18
+ * 
19
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22
+ * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
23
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
+ * SUCH DAMAGE.
31
+ */
32
+
33
+#ifndef _cxxtools_encode_h_
34
+#define _cxxtools_encode_h_
35
+
36
+// standard includes
37
+#include <string>
38
+
39
+namespace Cxxtools {
40
+namespace Encode {
41
+
42
+    //####################################################################
43
+    /** 
44
+     * Base64 encode the given string.
45
+     *
46
+     * @param s The string to encode.
47
+     * @return The encoded string.
48
+     * @author Peter Jones
49
+    **/
50
+    //####################################################################
51
+    std::string base64_encode (const std::string &s);
52
+
53
+    //####################################################################
54
+    /** 
55
+     * Decode the given Base64 string.
56
+     *
57
+     * @param s The string to decode.
58
+     * @return The decoded string.
59
+     * @author Peter Jones
60
+    **/
61
+    //####################################################################
62
+    std::string base64_decode (const std::string &s);
63
+
64
+    //####################################################################
65
+    /** 
66
+     * Digest the given string using the MD5 algorithm.
67
+     *
68
+     * @param s The string to digest.
69
+     * @return The result of digesting s with MD5.
70
+     * @author Peter Jones
71
+    **/
72
+    //####################################################################
73
+    std::string md5 (const std::string &s);
74
+
75
+    //####################################################################
76
+    /** 
77
+     * Digest the given string using the SHA1 algorithm.
78
+     *
79
+     * @param s The string to digest.
80
+     * @return The result of digesting s with SHA1.
81
+     * @author Peter Jones
82
+    **/
83
+    //####################################################################
84
+    std::string sha1 (const std::string &s);
85
+
86
+}} // end Cxxtools and Encode namespaces
87
+#endif

+ 144
- 0
src/encode.cxx View File

@@ -0,0 +1,144 @@
1
+/*
2
+ * Copyright (C) 2002-2006 Peter J Jones (pjones@pmade.org)
3
+ * All Rights Reserved
4
+ * 
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ * 
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ *    notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ *    notice, this list of conditions and the following disclaimer in
13
+ *    the documentation and/or other materials provided with the
14
+ *    distribution.
15
+ * 3. Neither the name of the Author nor the names of its contributors
16
+ *    may be used to endorse or promote products derived from this software
17
+ *    without specific prior written permission.
18
+ * 
19
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22
+ * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
23
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
+ * SUCH DAMAGE.
31
+ */
32
+
33
+// Cxxtools includes
34
+#include "cxxtools/encode.h"
35
+
36
+// OpenSSL includes
37
+#include <openssl/buffer.h>
38
+#include <openssl/bio.h>
39
+#include <openssl/evp.h>
40
+#include <openssl/md5.h>
41
+#include <openssl/sha.h>
42
+
43
+// standard includes
44
+#include <new>
45
+
46
+//####################################################################
47
+namespace
48
+{
49
+    const char hex[] = "0123456789abcdef";
50
+
51
+    struct auto_bio {
52
+	auto_bio (BIO *b) : bio(b)
53
+	{ if (!bio) throw std::bad_alloc(); }
54
+
55
+	~auto_bio (void)
56
+	{ if (bio) BIO_free(bio); }
57
+
58
+	void release (void)
59
+	{ bio = 0; }
60
+
61
+	BIO *bio;
62
+    };
63
+
64
+    void bin2hex (const unsigned char *hash, unsigned char *buf, std::size_t len);
65
+}
66
+//####################################################################
67
+std::string Cxxtools::Encode::base64_encode (const std::string &s)
68
+{
69
+    auto_bio b64(BIO_new(BIO_f_base64()));
70
+    auto_bio mem(BIO_new(BIO_s_mem()));
71
+    BIO *bio = BIO_push(b64.bio, mem.bio);
72
+    BIO_set_flags(b64.bio, BIO_FLAGS_BASE64_NO_NL);
73
+
74
+    if (BIO_write(bio, s.c_str(), s.size()) <= 0) return std::string();
75
+    BIO_flush(bio);
76
+
77
+    BUF_MEM *bptr;
78
+    std::string out;
79
+
80
+    BIO_get_mem_ptr(bio, &bptr);
81
+    out.assign(bptr->data, bptr->length);
82
+
83
+    return out;
84
+}
85
+//####################################################################
86
+std::string Cxxtools::Encode::base64_decode (const std::string &s)
87
+{
88
+    /* 
89
+     * We have to do a const_cast in this function because the OpenSSL
90
+     * interface wants a void* even though it is read-only.
91
+     */
92
+
93
+    auto_bio b64(BIO_new(BIO_f_base64()));
94
+    auto_bio mem(BIO_new_mem_buf(const_cast<char*>(s.c_str()), s.size()));
95
+    BIO *bio = BIO_push(b64.bio, mem.bio);
96
+    BIO_set_flags(b64.bio, BIO_FLAGS_BASE64_NO_NL);
97
+
98
+    char buf[256];
99
+    int len;
100
+    std::string out;
101
+    
102
+    while ( (len = BIO_read(bio, buf, sizeof(buf))) > 0)
103
+	out.append(buf, len);
104
+
105
+    return out;
106
+}
107
+//####################################################################
108
+std::string Cxxtools::Encode::md5 (const std::string &s)
109
+{
110
+    unsigned char buf1[MD5_DIGEST_LENGTH];
111
+    unsigned char buf2[MD5_DIGEST_LENGTH * 2 + 1];
112
+
113
+    MD5(reinterpret_cast<const unsigned char*>(s.c_str()), s.size(), buf1);
114
+    bin2hex(buf1, buf2, sizeof(buf1));
115
+
116
+    return std::string(reinterpret_cast<const char*>(buf2));
117
+}
118
+//####################################################################
119
+std::string Cxxtools::Encode::sha1 (const std::string &s)
120
+{
121
+    unsigned char buf1[SHA_DIGEST_LENGTH];
122
+    unsigned char buf2[SHA_DIGEST_LENGTH * 2 + 1];
123
+
124
+    SHA1(reinterpret_cast<const unsigned char*>(s.c_str()), s.size(), buf1);
125
+    bin2hex(buf1, buf2, sizeof(buf1));
126
+
127
+    return std::string(reinterpret_cast<const char*>(buf2));
128
+}
129
+//####################################################################
130
+namespace
131
+{
132
+    //####################################################################
133
+    void bin2hex (const unsigned char *hash, unsigned char *buf, std::size_t len)
134
+    {
135
+	for (std::size_t i=0; i!=len; ++i) {
136
+	    buf[i+i]    = hex[hash[i] >> 4];
137
+	    buf[i+i+1]  = hex[hash[i] & 0x0f];
138
+	}
139
+
140
+	buf[len * 2] = 0;
141
+    }
142
+    //####################################################################
143
+}
144
+//####################################################################

Loading…
Cancel
Save