ipc/chromium/src/base/hmac.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/hmac.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,56 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +// Utility class for calculating the HMAC for a given message. We currently
     1.9 +// only support SHA1 for the hash algorithm, but this can be extended easily.
    1.10 +
    1.11 +#ifndef BASE_HMAC_H_
    1.12 +#define BASE_HMAC_H_
    1.13 +
    1.14 +#include <string>
    1.15 +
    1.16 +#include "base/basictypes.h"
    1.17 +#include "base/scoped_ptr.h"
    1.18 +
    1.19 +namespace base {
    1.20 +
    1.21 +// Simplify the interface and reduce includes by abstracting out the internals.
    1.22 +struct HMACPlatformData;
    1.23 +
    1.24 +class HMAC {
    1.25 + public:
    1.26 +  // The set of supported hash functions. Extend as required.
    1.27 +  enum HashAlgorithm {
    1.28 +    SHA1
    1.29 +  };
    1.30 +
    1.31 +  explicit HMAC(HashAlgorithm hash_alg);
    1.32 +  ~HMAC();
    1.33 +
    1.34 +  // Initializes this instance using |key| of the length |key_length|. Call Init
    1.35 +  // only once. It returns false on the second or later calls.
    1.36 +  bool Init(const unsigned char* key, int key_length);
    1.37 +
    1.38 +  // Initializes this instance using |key|. Call Init only once. It returns
    1.39 +  // false on the second or later calls.
    1.40 +  bool Init(const std::string& key) {
    1.41 +    return Init(reinterpret_cast<const unsigned char*>(key.data()),
    1.42 +                static_cast<int>(key.size()));
    1.43 +  }
    1.44 +
    1.45 +  // Calculates the HMAC for the message in |data| using the algorithm supplied
    1.46 +  // to the constructor and the key supplied to the Init method. The HMAC is
    1.47 +  // returned in |digest|, which has |digest_length| bytes of storage available.
    1.48 +  bool Sign(const std::string& data, unsigned char* digest, int digest_length);
    1.49 +
    1.50 + private:
    1.51 +  HashAlgorithm hash_alg_;
    1.52 +  scoped_ptr<HMACPlatformData> plat_;
    1.53 +
    1.54 +  DISALLOW_COPY_AND_ASSIGN(HMAC);
    1.55 +};
    1.56 +
    1.57 +}  // namespace base
    1.58 +
    1.59 +#endif  // BASE_HMAC_H_

mercurial