blob: 1f858951944e7a98cf68b7d98f45a528ab46218f [file] [log] [blame]
Tao Baoe6aa3322015-08-05 15:20:27 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef RECOVERY_PRINT_SHA1_H
18#define RECOVERY_PRINT_SHA1_H
19
20#include <stdint.h>
21#include <string>
22
Tao Baoac9d94d2016-11-03 11:37:15 -070023#include <openssl/sha.h>
Tao Baoe6aa3322015-08-05 15:20:27 -070024
Tao Baoe1792762016-04-19 22:31:01 -070025static std::string print_sha1(const uint8_t* sha1, size_t len) {
Tao Baoe6aa3322015-08-05 15:20:27 -070026 const char* hex = "0123456789abcdef";
27 std::string result = "";
28 for (size_t i = 0; i < len; ++i) {
29 result.push_back(hex[(sha1[i]>>4) & 0xf]);
30 result.push_back(hex[sha1[i] & 0xf]);
31 }
32 return result;
33}
34
Sen Jiangc48cb5e2016-02-04 16:23:21 +080035static std::string print_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) {
36 return print_sha1(sha1, SHA_DIGEST_LENGTH);
Tao Baoe6aa3322015-08-05 15:20:27 -070037}
38
Sen Jiangc48cb5e2016-02-04 16:23:21 +080039static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) {
Tao Baoe6aa3322015-08-05 15:20:27 -070040 return print_sha1(sha1, 4);
41}
42
Tao Baoe1792762016-04-19 22:31:01 -070043static std::string print_hex(const uint8_t* bytes, size_t len) {
Tao Baoac9d94d2016-11-03 11:37:15 -070044 return print_sha1(bytes, len);
Tao Baoe1792762016-04-19 22:31:01 -070045}
46
Tao Baoe6aa3322015-08-05 15:20:27 -070047#endif // RECOVERY_PRINT_SHA1_H