blob: 882411b406d4612cd891c6ca33de1165346b6273 [file] [log] [blame]
bowgotsaib51722b2017-01-11 22:21:38 +08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __CORE_FS_MGR_PRIV_SHA_H
18#define __CORE_FS_MGR_PRIV_SHA_H
19
20#include <openssl/sha.h>
21
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080022class SHA256Hasher {
bowgotsaib51722b2017-01-11 22:21:38 +080023 private:
24 SHA256_CTX sha256_ctx;
25 uint8_t hash[SHA256_DIGEST_LENGTH];
26
27 public:
28 enum { DIGEST_SIZE = SHA256_DIGEST_LENGTH };
29
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080030 SHA256Hasher() { SHA256_Init(&sha256_ctx); }
bowgotsaib51722b2017-01-11 22:21:38 +080031
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080032 void update(const void* data, size_t data_size) { SHA256_Update(&sha256_ctx, data, data_size); }
bowgotsaib51722b2017-01-11 22:21:38 +080033
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080034 const uint8_t* finalize() {
bowgotsaib51722b2017-01-11 22:21:38 +080035 SHA256_Final(hash, &sha256_ctx);
36 return hash;
37 }
38};
39
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080040class SHA512Hasher {
bowgotsaib51722b2017-01-11 22:21:38 +080041 private:
42 SHA512_CTX sha512_ctx;
43 uint8_t hash[SHA512_DIGEST_LENGTH];
44
45 public:
46 enum { DIGEST_SIZE = SHA512_DIGEST_LENGTH };
47
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080048 SHA512Hasher() { SHA512_Init(&sha512_ctx); }
bowgotsaib51722b2017-01-11 22:21:38 +080049
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080050 void update(const uint8_t* data, size_t data_size) {
bowgotsaib51722b2017-01-11 22:21:38 +080051 SHA512_Update(&sha512_ctx, data, data_size);
52 }
53
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080054 const uint8_t* finalize() {
bowgotsaib51722b2017-01-11 22:21:38 +080055 SHA512_Final(hash, &sha512_ctx);
56 return hash;
57 }
58};
59
60#endif /* __CORE_FS_MGR_PRIV_SHA_H */