15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CRYPTO_SHA2_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CRYPTO_SHA2_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "crypto/crypto_export.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace crypto {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These functions perform SHA-256 operations.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Functions for SHA-384 and SHA-512 can be added when the need arises.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const size_t kSHA256Length = 32;  // Length in bytes of a SHA-256 hash.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Computes the SHA-256 hash of the input string 'str' and stores the first
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 'len' bytes of the hash in the output buffer 'output'.  If 'len' > 32,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// only 32 bytes (the full hash) are stored in the 'output' buffer.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CRYPTO_EXPORT void SHA256HashString(const base::StringPiece& str,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    void* output, size_t len);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Convenience version of the above that returns the result in a 32-byte
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// string.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CRYPTO_EXPORT std::string SHA256HashString(const base::StringPiece& str);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace crypto
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CRYPTO_SHA2_H_
34