changeset 213:1829c4266bee

Hash: hash functions
author David Demelier <markand@malikania.fr>
date Mon, 24 Mar 2014 13:54:28 +0100
parents 35e34b0b80d4
children 6c49e5e3ecc8
files C++/Hash.cpp C++/Hash.h
diffstat 2 files changed, 142 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Hash.cpp	Mon Mar 24 13:54:28 2014 +0100
@@ -0,0 +1,42 @@
+/*
+ * Hash.cpp -- hash functions
+ *
+ * Copyright (c) 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "Hash.h"
+
+#include <openssl/sha.h>
+#include <openssl/md5.h>
+
+std::string Hash::md5(const std::string &input)
+{
+	return convert<MD5_CTX, MD5_DIGEST_LENGTH>(input, MD5_Init, MD5_Update, MD5_Final);
+}
+
+std::string Hash::sha1(const std::string &input)
+{
+	return convert<SHA_CTX, SHA_DIGEST_LENGTH>(input, SHA1_Init, SHA1_Update, SHA1_Final);
+}
+
+std::string Hash::sha256(const std::string &input)
+{
+	return convert<SHA256_CTX, SHA256_DIGEST_LENGTH>(input, SHA256_Init, SHA256_Update, SHA256_Final);
+}
+
+std::string Hash::sha512(const std::string &input)
+{
+	return convert<SHA512_CTX, SHA512_DIGEST_LENGTH>(input, SHA512_Init, SHA512_Update, SHA512_Final);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Hash.h	Mon Mar 24 13:54:28 2014 +0100
@@ -0,0 +1,100 @@
+/*
+ * Hash.h -- hash functions
+ *
+ * Copyright (c) 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _HASH_H_
+#define _HASH_H_
+
+/**
+ * @file Hash.h
+ * @brief Hash functions
+ */
+
+#include <string>
+
+/**
+ * @class Hash
+ * @brief Hash functions
+ *
+ * Provide support for MD5, SHA1, SHA256 and SHA512.
+ */
+class Hash {
+private:
+	template <typename Context>
+	using Init	= int (*)(Context *);
+
+	template <typename Context>
+	using Update	= int (*)(Context *, const void *, size_t);
+
+	template <typename Context>
+	using Final	= int (*)(unsigned char *, Context *);
+
+	template <typename Context, size_t Length>
+	static std::string convert(const std::string &input,
+				   Init<Context> init,
+				   Update<Context> update,
+				   Final<Context> finalize)
+	{
+		unsigned char digest[Length];
+		char hash[Length * 2 + 1];
+		
+		Context ctx;
+		init(&ctx);
+		update(&ctx, input.c_str(), input.length());
+		finalize(digest, &ctx);
+		
+		for (int i = 0; i < Length; i++)
+			sprintf(&hash[i * 2], "%02x", (unsigned int)digest[i]);
+		
+		return std::string(hash);
+	}
+
+public:
+	/**
+	 * Hash using MD5.
+	 *
+	 * @param input the input string
+	 * @return the hashed string
+	 */
+	static std::string md5(const std::string &input);
+
+	/**
+	 * Hash using SHA1.
+	 *
+	 * @param input the input string
+	 * @return the hashed string
+	 */
+	static std::string sha1(const std::string &input);
+
+	/**
+	 * Hash using SHA256.
+	 *
+	 * @param input the input string
+	 * @return the hashed string
+	 */
+	static std::string sha256(const std::string &input);
+
+	/**
+	 * Hash using SHA512.
+	 *
+	 * @param input the input string
+	 * @return the hashed string
+	 */
+	static std::string sha512(const std::string &input);
+};
+
+#endif // !_HASH_H_