1 | /* |
2 | This file is part of KittenDB/Engine Library. |
3 | |
4 | KittenDB/Engine Library is free software: you can redistribute it and/or modify |
5 | it under the terms of the GNU Lesser General Public License as published by |
6 | the Free Software Foundation, either version 2 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | KittenDB/Engine Library 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 Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public License |
15 | along with KittenDB/Engine Library. If not, see <http://www.gnu.org/licenses/>. |
16 | |
17 | Copyright 2016 Telegram Messenger Inc |
18 | 2016 Nikolai Durov |
19 | */ |
20 | |
21 | #include <assert.h> |
22 | #include "sha1.h" |
23 | |
24 | void sha1_starts (sha1_context *ctx) { |
25 | EVP_MD_CTX_init (ctx); |
26 | EVP_DigestInit_ex (ctx, EVP_sha1(), NULL); |
27 | } |
28 | |
29 | void sha1_update (sha1_context *ctx, const unsigned char *input, int ilen) { |
30 | EVP_DigestUpdate (ctx, input, ilen); |
31 | } |
32 | |
33 | void sha1_finish (sha1_context *ctx, unsigned char output[20]) { |
34 | unsigned olen = 0; |
35 | EVP_DigestFinal_ex (ctx, output, &olen); |
36 | assert (olen == 20); |
37 | } |
38 | |
39 | void sha1 (const unsigned char *input, int ilen, unsigned char output[20]) { |
40 | sha1_context *ctx = EVP_MD_CTX_new(); |
41 | sha1_starts (ctx); |
42 | sha1_update (ctx, input, ilen); |
43 | sha1_finish (ctx, output); |
44 | EVP_MD_CTX_free (ctx); |
45 | } |
46 | |
47 | void sha1_two_chunks (const unsigned char *input1, int ilen1, const unsigned char *input2, int ilen2, unsigned char output[20]) { |
48 | sha1_context *ctx = EVP_MD_CTX_new(); |
49 | sha1_starts (ctx); |
50 | sha1_update (ctx, input1, ilen1); |
51 | sha1_update (ctx, input2, ilen2); |
52 | sha1_finish (ctx, output); |
53 | EVP_MD_CTX_free (ctx); |
54 | } |
55 | |