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