1 /* 2 * Copyright (C) 2025 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 #ifndef HDC_CREDENTIAL_MESSAGE_H 16 #define HDC_CREDENTIAL_MESSAGE_H 17 18 #include <string> 19 #include "log.h" 20 #include "securec.h" 21 22 class CredentialMessage { 23 public: 24 CredentialMessage() = default; 25 explicit CredentialMessage(const std::string& messageStr); 26 void Init(const std::string& messageStr); 27 ~CredentialMessage(); GetMessageVersion()28 int GetMessageVersion() const { return messageVersion; } GetMessageMethodType()29 int GetMessageMethodType() const { return messageMethodType; } GetMessageBodyLen()30 int GetMessageBodyLen() const { return messageBodyLen; } GetMessageBody()31 const std::string& GetMessageBody() const { return messageBody; } 32 33 void SetMessageVersion(int version); SetMessageMethodType(int type)34 void SetMessageMethodType(int type) { messageMethodType = type; } SetMessageBodyLen(int len)35 void SetMessageBodyLen(int len) { messageBodyLen = len; } 36 void SetMessageBody(const std::string& body); 37 std::string Construct() const; 38 39 private: 40 int messageVersion = 0; 41 int messageMethodType = 0; 42 int messageBodyLen = 0; 43 std::string messageBody; 44 }; 45 46 bool IsNumeric(const std::string& str); 47 int StripLeadingZeros(const std::string& input); 48 std::string IntToStringWithPadding(int length, int maxLen); 49 std::vector<uint8_t> String2Uint8(const std::string& str, size_t len); 50 51 constexpr size_t MESSAGE_STR_MAX_LEN = 1024; 52 constexpr size_t MESSAGE_VERSION_POS = 0; 53 constexpr size_t MESSAGE_METHOD_POS = 1; 54 constexpr size_t MESSAGE_METHOD_LEN = 3; 55 constexpr size_t MESSAGE_LENGTH_POS = 4; 56 constexpr size_t MESSAGE_LENGTH_LEN = 4; 57 constexpr size_t MESSAGE_BODY_POS = 8; 58 59 enum V1MethodID { 60 METHOD_ENCRYPT = 1, 61 METHOD_DECRYPT, 62 }; 63 64 enum MethodVersion { 65 METHOD_VERSION_V1 = 1, 66 METHOD_VERSION_MAX = 9, 67 }; 68 69 #endif // HDC_CREDENTIAL_MESSAGE_H