blob: 5b53eea9e78c8fd994d6bc4e033388d5ddce80ca [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 {
Bowgo Tsai95c966a2017-03-30 18:42:54 +080023 private:
bowgotsaib51722b2017-01-11 22:21:38 +080024 SHA256_CTX sha256_ctx;
25 uint8_t hash[SHA256_DIGEST_LENGTH];
26
Bowgo Tsai95c966a2017-03-30 18:42:54 +080027 public:
bowgotsaib51722b2017-01-11 22:21:38 +080028 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 Tsai95c966a2017-03-30 18:42:54 +080032 void update(const uint8_t* data, size_t data_size) {
33 SHA256_Update(&sha256_ctx, data, data_size);
34 }
bowgotsaib51722b2017-01-11 22:21:38 +080035
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080036 const uint8_t* finalize() {
bowgotsaib51722b2017-01-11 22:21:38 +080037 SHA256_Final(hash, &sha256_ctx);
38 return hash;
39 }
40};
41
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080042class SHA512Hasher {
Bowgo Tsai95c966a2017-03-30 18:42:54 +080043 private:
bowgotsaib51722b2017-01-11 22:21:38 +080044 SHA512_CTX sha512_ctx;
45 uint8_t hash[SHA512_DIGEST_LENGTH];
46
Bowgo Tsai95c966a2017-03-30 18:42:54 +080047 public:
bowgotsaib51722b2017-01-11 22:21:38 +080048 enum { DIGEST_SIZE = SHA512_DIGEST_LENGTH };
49
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080050 SHA512Hasher() { SHA512_Init(&sha512_ctx); }
bowgotsaib51722b2017-01-11 22:21:38 +080051
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080052 void update(const uint8_t* data, size_t data_size) {
bowgotsaib51722b2017-01-11 22:21:38 +080053 SHA512_Update(&sha512_ctx, data, data_size);
54 }
55
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080056 const uint8_t* finalize() {
bowgotsaib51722b2017-01-11 22:21:38 +080057 SHA512_Final(hash, &sha512_ctx);
58 return hash;
59 }
60};
61
62#endif /* __CORE_FS_MGR_PRIV_SHA_H */