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