• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     * CalcBufferSha: calculate a buffer sha1 hash for given buffer
41     *
42     * This function read the buffer and calc the sha1 value ,
43     * The caller can pass a char hash_str[41] to get the hash string
44     * The return value: 0 means successful,others mean failed.
45     */
46     static int CalcBufferSha(const std::string& buffer, size_t bufSize, char *hash, size_t len);
47 
48 private:
49     static int ConvertToString(const unsigned char hash[SHA256_DIGEST_LENGTH], char *outstr, size_t len);
50 
51 private:
52     static const int HASH_BUFFER_SIZE = 4096;
53 };
54 }
55 }
56 #endif
57