• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 OHOS_DM_CRYPTO_H
17 #define OHOS_DM_CRYPTO_H
18 #include <string>
19 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
20 #include "dm_kv_info.h"
21 #endif
22 
23 #ifndef DM_EXPORT
24 #define DM_EXPORT __attribute__ ((visibility ("default")))
25 #endif // DM_EXPORT
26 
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 class Crypto {
31 public:
32     DM_EXPORT static void DmGenerateStrHash(const void *data, size_t dataSize,
33         unsigned char *outBuf, uint32_t outBufLen, uint32_t startIndex);
34     DM_EXPORT static std::string Sha256(const std::string &text, bool isUpper = false);
35     DM_EXPORT static std::string Sha256(const void *data, size_t size,
36         bool isUpper = false);
37     DM_EXPORT static int32_t ConvertHexStringToBytes(unsigned char *outBuf,
38         uint32_t outBufLen, const char *inBuf, uint32_t inLen);
39     // follow the dsoftbus udid hash policy, use the first 8 bytes of full udid hash,
40     // after convert to string, it it 16 bytes.
41     DM_EXPORT static int32_t GetUdidHash(const std::string &udid,
42         unsigned char *udidHash);
43     DM_EXPORT static std::string GetUdidHash(const std::string &udid);
44     // use the first 16 bytes of full tokenId hash
45     // after convert to string, it it 32 bytes.
46     DM_EXPORT static std::string GetTokenIdHash(const std::string &tokenId);
47     DM_EXPORT static std::string GetGroupIdHash(const std::string &groupId);
48     static int32_t GetSecRandom(uint8_t *out, size_t outLen);
49     static std::string GetSecSalt();
50     static std::string GetHashWithSalt(const std::string &text, const std::string &salt);
51     // follow the dsoftbus accountid hash policy, use the first 3 bytes of full accountid hash,
52     // after convert to string, it it 6 bytes.
53     DM_EXPORT static int32_t GetAccountIdHash(const std::string &accountId,
54         unsigned char *accountIdHash);
55     // use the first 16 bytes of full accountId hash
56     // after convert to string, it it 32 bytes.
57     DM_EXPORT static std::string GetAccountIdHash16(const std::string &accountId);
58     DM_EXPORT static int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen,
59         const unsigned char *inBuf, uint32_t inLen);
60 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
61     DM_EXPORT static int32_t ConvertUdidHashToAnoyAndSave(const std::string &appId,
62         const std::string &udidHash, DmKVValue &kvValue);
63     DM_EXPORT static int32_t ConvertUdidHashToAnoyDeviceId(const std::string &appId,
64         const std::string &udidHash, DmKVValue &kvValue);
65     static int32_t GetAnoyDeviceInfo(const std::string &appId, const std::string &udidHash, DmKVValue &kvValue);
66     static int32_t ConvertUdidHashToAnoyGenerate(const std::string &appId, const std::string &udidHash,
67         DmKVValue &kvValue);
68 #endif
69 };
70 } // namespace DistributedHardware
71 } // namespace OHOS
72 #endif // OHOS_DM_CRYPTO_H
73