• 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 #include "calc_fingerprint.h"
16 
17 #include <securec.h>
18 
19 #include "common_defines.h"
20 #include "file_util.h"
21 
22 using namespace std;
23 namespace OHOS {
24 namespace HiviewDFX {
25 DEFINE_LOG_TAG("CalcFingerprint");
ConvertToString(const unsigned char hash[SHA256_DIGEST_LENGTH],char * outstr,size_t len)26 int CalcFingerprint::ConvertToString(const unsigned char hash[SHA256_DIGEST_LENGTH], char *outstr, size_t len)
27 {
28     uint32_t i;
29     char *outHash = outstr;
30 
31     if (hash == nullptr || outHash == nullptr) {
32         return EINVAL;
33     }
34     constexpr int charsEachHex = 2;
35     if (len < (SHA256_DIGEST_LENGTH * charsEachHex + 1)) { // 1: add '\0'
36         return ENOMEM;
37     }
38     for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
39         int err = snprintf_s(outHash, charsEachHex + 1, charsEachHex, "%02x", hash[i]);
40         if (err < 0) {
41             return err;
42         }
43         outHash += charsEachHex;
44     }
45     *outHash = '\0';
46     return 0;
47 }
48 
49 /*
50  * API name : calc_file_sha1
51  * Description : calculate a file sha1 hash for given file
52  * Input parameters
53  * filePath : path of the file to be calculated
54  * hash      : buffer to store output sha1 string
55  * Return
56  * 0 : successful
57  * x : fail
58  */
CalcFileSha(const string & filePath,char * hash,size_t len)59 int CalcFingerprint::CalcFileSha(const string& filePath, char *hash, size_t len)
60 {
61     if (filePath.empty() || hash == nullptr || !FileUtil::IsLegalPath(filePath)) {
62         return EINVAL;
63     }
64     FILE *fp = nullptr;
65     fp = fopen(filePath.c_str(), "rb");
66     if (fp == nullptr) {
67         return errno; // if file not exist, errno will be ENOENT
68     }
69 
70     size_t n;
71     char buffer[HASH_BUFFER_SIZE] = {0};
72     unsigned char value[SHA256_DIGEST_LENGTH] = {0};
73     SHA256_CTX ctx;
74     SHA256_Init(&ctx);
75     while ((n = fread(buffer, 1, sizeof(buffer), fp))) {
76         SHA256_Update(&ctx, (unsigned char *)buffer, n);
77     }
78     if (fclose(fp)) {
79         HIVIEW_LOGE("fclose is failed");
80     }
81     fp = nullptr;
82     SHA256_Final(value, &ctx);
83     return ConvertToString(value, hash, len);
84 }
85 
86 /*
87  * API name : calc_buffer_sha1
88  * Description : calculate a buffer sha1 hash for given buffer
89  * Input parameters
90  * buffer : buffer to store the content which needed to be calculated
91  * hash_str   : buffer to store output sha1 string
92  * Return
93  * 0 : successful
94  * x : fail
95  */
CalcBufferSha(const string & buffer,size_t bufSize,char * hash,size_t len)96 int CalcFingerprint::CalcBufferSha(const string& buffer, size_t bufSize, char *hash, size_t len)
97 {
98     if (buffer.empty() || hash == nullptr) {
99         return EINVAL;
100     }
101     unsigned char value[SHA256_DIGEST_LENGTH] = {0};
102     SHA256((unsigned char *)buffer.c_str(), bufSize, value);
103     return ConvertToString(value, hash, len);
104 }
105 }
106 }
107