• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #define LOG_TAG "UdmfUtils"
16 
17 #include "udmf_utils.h"
18 #include <random>
19 #include <sstream>
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "logger.h"
23 
24 namespace OHOS {
25 namespace UDMF {
26 namespace UTILS {
27 static constexpr int ID_LEN = 32;
28 static constexpr int MINIMUM = 48;
29 static constexpr int MAXIMUM = 121;
30 constexpr char SPECIAL = '^';
31 
StrSplit(const std::string & str,const std::string & delimiter)32 std::vector<std::string> StrSplit(const std::string &str, const std::string &delimiter)
33 {
34     std::vector<std::string> result;
35     size_t start = 0;
36     size_t end = str.find(delimiter);
37     while (end != std::string::npos) {
38         result.push_back(str.substr(start, end - start));
39         start = end + delimiter.length();
40         end = str.find(delimiter, start);
41     }
42     result.push_back(str.substr(start));
43     return result;
44 }
45 
Random(int32_t len,int32_t minimum,int32_t maximum)46 std::vector<uint8_t> Random(int32_t len, int32_t minimum, int32_t maximum)
47 {
48     std::random_device randomDevice;
49     std::uniform_int_distribution<int> distribution(minimum, maximum);
50     std::vector<uint8_t> key(len);
51     for (int32_t i = 0; i < len; i++) {
52         key[i] = static_cast<uint8_t>(distribution(randomDevice));
53     }
54     return key;
55 }
56 
GenerateId()57 std::string GenerateId()
58 {
59     std::vector<uint8_t> randomDevices = Random(ID_LEN, MINIMUM, MAXIMUM);
60     std::stringstream idStr;
61     for (auto &randomDevice : randomDevices) {
62         auto asc = randomDevice;
63         asc = asc >= SPECIAL ? asc + 1 : asc;
64         idStr << static_cast<uint8_t>(asc);
65     }
66     return idStr.str();
67 }
68 
GetCurrentSdkVersion()69 std::string GetCurrentSdkVersion()
70 {
71     static const std::string sdkVersion = GetSdkVersionByToken(IPCSkeleton::GetSelfTokenID());
72     return sdkVersion;
73 }
74 
IsTokenNative()75 bool IsTokenNative()
76 {
77     uint32_t tokenId = IPCSkeleton::GetSelfTokenID();
78     if (tokenId == 0) {
79         return false;
80     }
81     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
82     LOG_DEBUG(UDMF_FRAMEWORK, "tokenId=%{public}u, tokenType=%{public}d", tokenId, tokenType);
83     return tokenType == Security::AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE;
84 }
85 
86 } // namespace UTILS
87 } // namespace UDMF
88 } // namespace OHOS