Replace boost::scoped_ptr with std::unique_ptr.
[cumulus.git] / third_party / sha1.cc
index 7111f94..a7390b3 100644 (file)
@@ -1,9 +1,12 @@
 /* sha1.cc - Functions to compute SHA1 message digest of data streams
  * according to the NIST specification FIPS-180-1.
- * part of Cumulus: Smart Filesystem Backup to Dumb Servers
+ * part of Cumulus: Efficient Filesystem Backup to the Cloud
  *
  * Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
  * Copyright (C) 2006-2007  The Regents of the University of California
+ * Copyright (C) 2012 The Cumulus Developers
+ * See the AUTHORS file for a list of Cumulus contributors.
+ *
  * Written by Scott G. Miller
  * Additional Credits:
  *    Robert Klep <robert@ilse.nl>  -- Expansion function fix
@@ -27,6 +30,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "../hash.h"
 #include "sha1.h"
 
 #include <stddef.h>
@@ -150,7 +154,7 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
     {
 #if !_STRING_ARCH_unaligned
 # define alignof(type) offsetof (struct { char c; type x; }, x)
-# define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0)
+# define UNALIGNED_P(p) (1)
       if (UNALIGNED_P (buffer))
        while (len > 64)
          {
@@ -392,3 +396,40 @@ string SHA1Checksum::checksum_str()
 
     return result;
 }
+
+class SHA1Hash : public Hash {
+public:
+    SHA1Hash();
+    static Hash *New() { return new SHA1Hash; }
+    virtual void update(const void *data, size_t len);
+    virtual size_t digest_size() const { return 20; }
+    virtual std::string name() const { return "sha1"; }
+
+protected:
+    const uint8_t *finalize();
+
+private:
+    struct sha1_ctx ctx;
+    char resbuf[20] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
+};
+
+SHA1Hash::SHA1Hash()
+{
+    sha1_init_ctx(&ctx);
+}
+
+void SHA1Hash::update(const void *data, size_t len)
+{
+    sha1_process_bytes(data, len, &ctx);
+}
+
+const uint8_t *SHA1Hash::finalize()
+{
+    sha1_finish_ctx(&ctx, resbuf);
+    return (const uint8_t *)resbuf;
+}
+
+void sha1_register()
+{
+    Hash::Register("sha1", SHA1Hash::New);
+}