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 "sha256.h"
23
24void sha256_starts (sha256_context *ctx) {
25 EVP_MD_CTX_init (ctx);
26 EVP_DigestInit_ex (ctx, EVP_sha256(), NULL);
27}
28
29void sha256_update (sha256_context *ctx, const unsigned char *input, int ilen) {
30 EVP_DigestUpdate (ctx, input, ilen);
31}
32
33void sha256_finish (sha256_context *ctx, unsigned char output[32]) {
34 unsigned olen = 0;
35 EVP_DigestFinal_ex (ctx, output, &olen);
36 assert (olen == 32);
37}
38
39void sha256 (const unsigned char *input, int ilen, unsigned char output[32]) {
40 sha256_context *ctx = EVP_MD_CTX_new();
41 sha256_starts (ctx);
42 sha256_update (ctx, input, ilen);
43 sha256_finish (ctx, output);
44 EVP_MD_CTX_free (ctx);
45}
46
47void sha256_two_chunks (const unsigned char *input1, int ilen1, const unsigned char *input2, int ilen2, unsigned char output[32]) {
48 sha256_context *ctx = EVP_MD_CTX_new();
49 sha256_starts (ctx);
50 sha256_update (ctx, input1, ilen1);
51 sha256_update (ctx, input2, ilen2);
52 sha256_finish (ctx, output);
53 EVP_MD_CTX_free (ctx);
54}
55