1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UTIL_CRYPTO_SHA2_H_
6 #define UTIL_CRYPTO_SHA2_H_
7 
8 #include <openssl/sha.h>
9 #include <stddef.h>
10 
11 #include <string>
12 
13 #include "absl/strings/string_view.h"
14 #include "platform/base/error.h"
15 
16 namespace openscreen {
17 
18 // These functions perform SHA-256 operations.
19 //
20 // Functions for SHA-384 and SHA-512 can be added when the need arises.
21 
22 // Computes the SHA-256 hash of the input string 'str' and stores the first
23 // 'len' bytes of the hash in the output buffer 'output'.  If 'len' > 32,
24 // only 32 bytes (the full hash) are stored in the 'output' buffer.
25 Error SHA256HashString(absl::string_view str,
26                        uint8_t output[SHA256_DIGEST_LENGTH]);
27 
28 // Convenience version of the above that returns the result in a 32-byte
29 // string.
30 ErrorOr<std::string> SHA256HashString(absl::string_view str);
31 
32 }  // namespace openscreen
33 
34 #endif  // UTIL_CRYPTO_SHA2_H_
35