1 /*
2  * Copyright (c) 2024-2024 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 SIGNATRUETOOLS_HASH_UTILS_H
17 #define SIGNATRUETOOLS_HASH_UTILS_H
18 
19 #include <string>
20 #include <vector>
21 
22 namespace OHOS {
23 namespace SignatureTools {
24 enum class HashAlgs {
25     USE_NONE = 0,
26     USE_MD2 = 1,
27     USE_MD4 = 2,
28     USE_MD5 = 3,
29     USE_SHA1 = 4,
30     USE_SHA224 = 5,
31     USE_SHA256 = 6,
32     USE_SHA384 = 7,
33     USE_SHA512 = 8,
34     USE_RIPEMD160 = 9,
35 };
36 
37 class HashUtils {
38 public:
39     HashUtils() = delete;
40     static int GetHashAlgsId(const std::string& algMethod);
41     static std::vector<int8_t> GetFileDigest(const std::string& inputFile, const std::string& algName);
42     static std::string GetHashAlgName(int algId);
43     static std::vector<int8_t> GetDigestFromBytes(const std::vector<int8_t>& fileBytes, int64_t length,
44                                                         const std::string& algName);
45 
46 private:
47     static std::vector<int8_t> GetByteDigest(const std::string& str, int count,
48                                                     const std::string& algMethod);
49 
50     static const int HASH_LEN = 4096;
51 };
52 } // namespace SignatureTools
53 } // namespace OHOS
54 #endif // SIGNATURETOOLS_HASH_UTILS_H