mfbt/SHA1.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/SHA1.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,62 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* Simple class for computing SHA1. */
    1.11 +
    1.12 +#ifndef mozilla_SHA1_h
    1.13 +#define mozilla_SHA1_h
    1.14 +
    1.15 +#include "mozilla/Types.h"
    1.16 +
    1.17 +#include <stddef.h>
    1.18 +#include <stdint.h>
    1.19 +
    1.20 +namespace mozilla {
    1.21 +
    1.22 +/**
    1.23 + * This class computes the SHA1 hash of a byte sequence, or of the concatenation
    1.24 + * of multiple sequences.  For example, computing the SHA1 of two sequences of
    1.25 + * bytes could be done as follows:
    1.26 + *
    1.27 + *   void SHA1(const uint8_t* buf1, uint32_t size1,
    1.28 + *             const uint8_t* buf2, uint32_t size2,
    1.29 + *             SHA1Sum::Hash& hash)
    1.30 + *   {
    1.31 + *     SHA1Sum s;
    1.32 + *     s.update(buf1, size1);
    1.33 + *     s.update(buf2, size2);
    1.34 + *     s.finish(hash);
    1.35 + *   }
    1.36 + *
    1.37 + * The finish method may only be called once and cannot be followed by calls
    1.38 + * to update.
    1.39 + */
    1.40 +class SHA1Sum
    1.41 +{
    1.42 +    union {
    1.43 +        uint32_t w[16]; /* input buffer */
    1.44 +        uint8_t b[64];
    1.45 +    } u;
    1.46 +    uint64_t size; /* count of hashed bytes. */
    1.47 +    unsigned H[22]; /* 5 state variables, 16 tmp values, 1 extra */
    1.48 +    bool mDone;
    1.49 +
    1.50 +  public:
    1.51 +    MFBT_API SHA1Sum();
    1.52 +
    1.53 +    static const size_t HashSize = 20;
    1.54 +    typedef uint8_t Hash[HashSize];
    1.55 +
    1.56 +    /* Add len bytes of dataIn to the data sequence being hashed. */
    1.57 +    MFBT_API void update(const void* dataIn, uint32_t len);
    1.58 +
    1.59 +    /* Compute the final hash of all data into hashOut. */
    1.60 +    MFBT_API void finish(SHA1Sum::Hash& hashOut);
    1.61 +};
    1.62 +
    1.63 +} /* namespace mozilla */
    1.64 +
    1.65 +#endif /* mozilla_SHA1_h */

mercurial