Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
This file is part of SLL.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
SLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLL; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "slm.hh"
#include "cypher.hh"
#include "error.hh"
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <openssl/md5.h>
/**
* return sha1 of data with base64 encoding
*
* @param data data to sha1 and base64
* @param size size of data
*
* @return malloc'ed sha1 size
*/
string Cypher::sha1_64(const char *data, size_t size) {
unsigned char md[SHA_DIGEST_LENGTH];
BIO *bmem, *b64;
BUF_MEM *bptr;
string ret;
// compute sha1
SHA1((const unsigned char *) data, size, md);
// compute b64
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, md, SHA_DIGEST_LENGTH);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
if (bptr->length > 0)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
else
throw Error(ERR_CYPHER, "Unable to compute sha1_64");
BIO_free_all(b64);
return ret;
}
/**
* return md5 hexa digest of file @param file.
*
* @return malloc'ed md5 digest in hexadecimal
*/
string Cypher::md5_16(const string &file) {
MD5_CTX ctx;
FILE *fs;
size_t len;
char buf[512];
char md[MD5_DIGEST_LENGTH];
char digest[MD5_DIGEST_LENGTH * 2 + 1];
if (!MD5_Init(&ctx))
throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file);
if ((fs = fopen(file.c_str(), "r")) == 0)
throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file);
while ((len = fread(buf, 1, 512, fs)) > 0)
if (!MD5_Update(&ctx, buf, len))
break;
if (!MD5_Final((unsigned char*)md, &ctx))
throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file);
for(len = 0; len < MD5_DIGEST_LENGTH; ++len)
sprintf(digest + (len * 2), "%02x", (unsigned char) md[len]);
digest[MD5_DIGEST_LENGTH * 2] = 0;
return digest;
}