1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CALC_FINGERPRINT_H 17 #define CALC_FINGERPRINT_H 18 19 #include <string> 20 #include <openssl/sha.h> 21 22 namespace OHOS { 23 namespace HiviewDFX { 24 class CalcFingerprint { 25 public: CalcFingerprint()26 CalcFingerprint() {}; ~CalcFingerprint()27 ~CalcFingerprint() {}; 28 29 public: 30 /* 31 * CalcFileSha: calculate a file sha1 hash for given file 32 * 33 * This function read the file and calc the sha1 value , 34 * The caller can pass a char hash_str[41] to get the hash string 35 * The return value: 0 means successful,others mean failed. 36 */ 37 static int CalcFileSha(const std::string& filePath, char *hash, size_t len); 38 39 /* 40 * CalcFileSha: calculate a file sha1 hash for given file 41 * 42 * This function read the file and calc the sha1 value , 43 * The caller can pass a char hash[41] to get the hash 44 * The return value: 0 means successful,others mean failed. 45 */ 46 static int CalcFileShaOriginal(const std::string& filePath, unsigned char *hash, size_t len); 47 48 /* 49 * CalcBufferSha: calculate a buffer sha1 hash for given buffer 50 * 51 * This function read the buffer and calc the sha1 value , 52 * The caller can pass a char hash_str[41] to get the hash string 53 * The return value: 0 means successful,others mean failed. 54 */ 55 static int CalcBufferSha(const std::string& buffer, size_t bufSize, char *hash, size_t len); 56 57 /* 58 * CalcBufferSha: calculate a source sha1 hash for given source 59 * 60 * This function read the source and calc the sha1 value , 61 * The caller can pass a char hash[41] to get the hash string 62 * The return value: 0 means successful,others mean failed. 63 */ 64 static int CalcBufferSha(unsigned char* source, size_t sourceLen, char *hash, size_t hashLen); 65 66 private: 67 static int ConvertToString(const unsigned char hash[SHA256_DIGEST_LENGTH], char *outstr, size_t len); 68 69 private: 70 static const int HASH_BUFFER_SIZE = 4096; 71 }; 72 } 73 } 74 #endif 75