1 /* 2 * Copyright (c) 2021 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_DEVICE_MANAGER_ENCRYPT_UTILS_H 17 #define OHOS_DEVICE_MANAGER_ENCRYPT_UTILS_H 18 19 #include <cstdint> 20 #include <stdlib.h> 21 22 namespace OHOS { 23 namespace DistributedHardware { 24 class EncryptUtils { 25 public: 26 static int32_t MbedtlsBase64Encode(uint8_t *dst, size_t dlen, size_t *olen, 27 const uint8_t *src, size_t slen); 28 static int32_t MbedtlsBase64Decode(uint8_t *dst, size_t dlen, size_t *olen, 29 const uint8_t *src, size_t slen); 30 static int32_t GenRandInt(int32_t randMin, int32_t randMax); 31 static int64_t GenRandLongLong(int64_t randMin, int64_t randMax); 32 static int32_t GetRandomData(uint8_t *randStr, uint32_t len); 33 static int32_t MbedtlsEncrypt(const uint8_t *plainText, int32_t plainTextLen, uint8_t *cipherText, 34 int32_t cipherTextLen, int32_t *outLen); 35 static int32_t MbedtlsDecrypt(const uint8_t *cipherText, int32_t cipherTextLen, uint8_t *plainText, 36 int32_t plainTextLen, int32_t *outLen); 37 /** 38 * szOut最后一位为结束符,比如 szOutLen=4 可能返回 "abc" 39 * @param szOutLen 至少2 40 * @param numberOnly 是否只生成数据 如果为true 则期望返回随机数字 如果为否,则期望返回随机字符 41 */ 42 static bool MbedtlsGenRandomStr(char *szOut, int32_t szOutLen, bool numberOnly); 43 }; 44 } // namespace DistributedHardware 45 } // namespace OHOS 46 #endif // OHOS_DEVICE_MANAGER_ENCRYPT_UTILS_H