push
[NextIRCd.git] / src / crypt_md5.c
1 /* crypt_md5.c - NextIRCd
2  * Copyright (C) 2012-2013  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "crypt_md5.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #if defined(__APPLE__)
23 #define COMMON_DIGEST_FOR_OPENSSL
24 #include <CommonCrypto/CommonDigest.h>
25 #define SHA1 CC_SHA1
26 #else
27 #include <openssl/md5.h>
28 #endif
29
30 unsigned char *crypt_md5_bin(const char *str, int strlen) {
31         int n;
32         MD5_CTX c;
33         unsigned char *digest = (char*)malloc(17);
34         
35         MD5_Init(&c);
36         
37         while (strlen > 0) {
38                 if (strlen > 512) {
39                         MD5_Update(&c, str, 512);
40                 } else {
41                         MD5_Update(&c, str, strlen);
42                 }
43                 strlen -= 512;
44                 str += 512;
45         }
46         
47         MD5_Final(digest, &c);
48         digest[16] = 0;
49         
50         return digest;
51 }