1 /*
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 
22 class SHA256Hasher {
23   private:
24     SHA256_CTX sha256_ctx;
25     uint8_t hash[SHA256_DIGEST_LENGTH];
26 
27   public:
28     enum { DIGEST_SIZE = SHA256_DIGEST_LENGTH };
29 
SHA256Hasher()30     SHA256Hasher() { SHA256_Init(&sha256_ctx); }
31 
update(const uint8_t * data,size_t data_size)32     void update(const uint8_t* data, size_t data_size) {
33         SHA256_Update(&sha256_ctx, data, data_size);
34     }
35 
finalize()36     const uint8_t* finalize() {
37         SHA256_Final(hash, &sha256_ctx);
38         return hash;
39     }
40 };
41 
42 class SHA512Hasher {
43   private:
44     SHA512_CTX sha512_ctx;
45     uint8_t hash[SHA512_DIGEST_LENGTH];
46 
47   public:
48     enum { DIGEST_SIZE = SHA512_DIGEST_LENGTH };
49 
SHA512Hasher()50     SHA512Hasher() { SHA512_Init(&sha512_ctx); }
51 
update(const uint8_t * data,size_t data_size)52     void update(const uint8_t* data, size_t data_size) {
53         SHA512_Update(&sha512_ctx, data, data_size);
54     }
55 
finalize()56     const uint8_t* finalize() {
57         SHA512_Final(hash, &sha512_ctx);
58         return hash;
59     }
60 };
61 
62 #endif /* __CORE_FS_MGR_PRIV_SHA_H */
63