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