1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "crypto.h"
17 #include <iomanip>
18 #include <openssl/evp.h>
19 #include <sstream>
20 #include <vector>
21 #include "common/sharing_log.h"
22
23 namespace OHOS {
24 namespace Sharing {
25
GenerateSha256HashId(const uint8_t * originalId,uint32_t & len)26 std::vector<uint8_t> GenerateSha256HashId(const uint8_t *originalId, uint32_t &len)
27 {
28 if (originalId == nullptr || len == 0) {
29 return std::vector<uint8_t>{};
30 }
31
32 std::unique_ptr<uint8_t[]> outHash = std::make_unique<uint8_t[]>(len);
33 uint32_t outLen = 0; // should be 32 bytes
34 int32_t ret = EVP_Digest(originalId, len, outHash.get(), &outLen, EVP_sha256(), nullptr);
35 if (ret != 1) {
36 SHARING_LOGE("Get Openssl Hash fail, %{public}d", ret);
37 return std::vector<uint8_t>{};
38 }
39 std::vector<uint8_t> result(outHash.get(), outHash.get() + outLen);
40 return result;
41 }
42 } // namespace Sharing
43 } // namespace OHOS