changeset 394:fdceef4be88b

Hash: remove class, use namespace
author David Demelier <markand@malikania.fr>
date Mon, 28 Sep 2015 15:45:35 +0200
parents 875b5ce19fda
children b78d6d8f2872
files C++/modules/Hash/Hash.cpp C++/modules/Hash/Hash.h C++/tests/Hash/main.cpp
diffstat 3 files changed, 70 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/C++/modules/Hash/Hash.cpp	Mon Sep 28 15:16:31 2015 +0200
+++ b/C++/modules/Hash/Hash.cpp	Mon Sep 28 15:45:35 2015 +0200
@@ -21,22 +21,56 @@
 #include <openssl/sha.h>
 #include <openssl/md5.h>
 
-std::string Hash::md5(const std::string &input)
+namespace hash {
+
+namespace {
+
+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>
+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 (unsigned long i = 0; i < Length; i++)
+		sprintf(&hash[i * 2], "%02x", (unsigned int)digest[i]);
+	
+	return std::string(hash);
+}
+
+} // !namespace
+
+std::string 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)
+std::string 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)
+std::string 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)
+std::string sha512(const std::string &input)
 {
 	return convert<SHA512_CTX, SHA512_DIGEST_LENGTH>(input, SHA512_Init, SHA512_Update, SHA512_Final);
 }
+
+} // !hash
--- a/C++/modules/Hash/Hash.h	Mon Sep 28 15:16:31 2015 +0200
+++ b/C++/modules/Hash/Hash.h	Mon Sep 28 15:45:35 2015 +0200
@@ -26,75 +26,40 @@
 
 #include <string>
 
+namespace hash {
+
 /**
- * @class Hash
- * @brief Hash functions
+ * Hash using MD5.
  *
- * Provide support for MD5, SHA1, SHA256 and SHA512.
+ * @param input the input string
+ * @return the hashed string
  */
-class Hash {
-private:
-	template <typename Context>
-	using Init	= int (*)(Context *);
+std::string md5(const std::string &input);
 
-	template <typename Context>
-	using Update	= int (*)(Context *, const void *, size_t);
-
-	template <typename Context>
-	using Final	= int (*)(unsigned char *, Context *);
+/**
+ * Hash using SHA1.
+ *
+ * @param input the input string
+ * @return the hashed string
+ */
+std::string sha1(const std::string &input);
 
-	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 (unsigned long i = 0; i < Length; i++)
-			sprintf(&hash[i * 2], "%02x", (unsigned int)digest[i]);
-		
-		return std::string(hash);
-	}
+/**
+ * Hash using SHA256.
+ *
+ * @param input the input string
+ * @return the hashed string
+ */
+std::string sha256(const std::string &input);
 
-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 SHA512.
+ *
+ * @param input the input string
+ * @return the hashed string
+ */
+std::string sha512(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);
-};
+} // !hash
 
 #endif // !_HASH_H_
--- a/C++/tests/Hash/main.cpp	Mon Sep 28 15:16:31 2015 +0200
+++ b/C++/tests/Hash/main.cpp	Mon Sep 28 15:45:35 2015 +0200
@@ -27,7 +27,7 @@
 TEST(Hash, md5)
 {
 	std::string expected = "b10a8db164e0754105b7a99be72e3fe5";
-	std::string output = Hash::md5("Hello World");
+	std::string output = hash::md5("Hello World");
 
 	ASSERT_EQ(expected, output);
 }
@@ -35,7 +35,7 @@
 TEST(Hash, sha1)
 {
 	std::string expected = "0a4d55a8d778e5022fab701977c5d840bbc486d0";
-	std::string output = Hash::sha1("Hello World");
+	std::string output = hash::sha1("Hello World");
 
 	ASSERT_EQ(expected, output);
 }
@@ -43,7 +43,7 @@
 TEST(Hash, sha256)
 {
 	std::string expected = "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e";
-	std::string output = Hash::sha256("Hello World");
+	std::string output = hash::sha256("Hello World");
 
 	ASSERT_EQ(expected, output);
 }
@@ -51,7 +51,7 @@
 TEST(Hash, sha512)
 {
 	std::string expected = "2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b";
-	std::string output = Hash::sha512("Hello World");
+	std::string output = hash::sha512("Hello World");
 
 	ASSERT_EQ(expected, output);
 }