• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "types_export.h"
17 
18 #include <algorithm>
19 #include <cstring>
20 
21 namespace DistributedDB {
22 const size_t CipherPassword::MAX_PASSWORD_SIZE;
23 
CipherPassword()24 CipherPassword::CipherPassword()
25 {}
26 
~CipherPassword()27 CipherPassword::~CipherPassword()
28 {
29     (void)Clear();
30 }
31 
operator ==(const CipherPassword & input) const32 bool CipherPassword::operator==(const CipherPassword &input) const
33 {
34     if (size_ != input.GetSize()) {
35         return false;
36     }
37     return memcmp(data_, input.GetData(), size_) == 0;
38 }
39 
operator !=(const CipherPassword & input) const40 bool CipherPassword::operator!=(const CipherPassword &input) const
41 {
42     return !(*this == input);
43 }
44 
GetSize() const45 size_t CipherPassword::GetSize() const
46 {
47     return size_;
48 }
49 
GetData() const50 const uint8_t* CipherPassword::GetData() const
51 {
52     return data_;
53 }
54 
SetValue(const uint8_t * inputData,size_t inputSize)55 int CipherPassword::SetValue(const uint8_t *inputData, size_t inputSize)
56 {
57     if (inputSize > MAX_PASSWORD_SIZE) {
58         return ErrorCode::OVERSIZE;
59     }
60     if (inputSize != 0 && inputData == nullptr) {
61         return ErrorCode::INVALID_INPUT;
62     }
63 
64     if (inputSize != 0) {
65         std::copy(inputData, inputData + inputSize, data_);
66     }
67 
68     size_t filledSize = std::min(size_, MAX_PASSWORD_SIZE);
69     if (inputSize < filledSize) {
70         std::fill(data_ + inputSize, data_ + filledSize, UCHAR_MAX);
71     }
72 
73     size_ = inputSize;
74     return ErrorCode::OK;
75 }
76 
Clear()77 int CipherPassword::Clear()
78 {
79     return SetValue(nullptr, 0);
80 }
81 } // namespace DistributedDB