Verified Commit b1d0e132 authored by RobLoach's avatar RobLoach
Browse files

data: Add love.data.hash('md5', data)

parent 44bf6da2
......@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Android build
- By [@webgeek1234](https://github.com/webgeek1234)
- `love.data.hash()` to retrieve MD5 hashes from content
## 0.18.1 - 2018-04-09
### Added
......
......@@ -139,6 +139,10 @@ FLAGS += -I$(CORE_DIR)/vendor/Snippets
# STB
FLAGS += -I$(CORE_DIR)/vendor/stb
# Hash Library
FLAGS += -I$(CORE_DIR)/vendor/hash-library
SOURCES_CXX += $(CORE_DIR)/vendor/hash-library/md5.cpp
# ChaiScript
ifeq ($(HAVE_CHAISCRIPT),)
FLAGS += -I$(CORE_DIR)/vendor/chaiscript/include
......
......@@ -6,6 +6,7 @@
#include <sstream>
#include <iostream>
#include "zlib.h"
#include "md5.h"
namespace love {
......@@ -91,4 +92,14 @@ std::string data::decompress(const std::string& str) {
return outstring;
}
std::string data::hash(const std::string& hashFunction, const std::string& data) {
if (hashFunction == "md5") {
MD5 md5;
return md5(data);
}
std::cout << "[ChaiLove] Error: Hash function not found: " << hashFunction << "." << std::endl;
return "";
}
} // namespace love
......@@ -32,6 +32,18 @@ class data {
* @see love.data.compress
*/
std::string decompress(const std::string& str);
/**
* Compute the message digest of specified string with specified algorithm.
*
* @param hashFunction Hash algorithm to use (md5).
* @param data String to hash.
*
* @todo Add sha1, sha256, sha512, etc.
*
* @return Raw message digest string.
*/
std::string hash(const std::string& hashFunction, const std::string& data);
};
} // namespace love
......
......@@ -335,6 +335,7 @@ script::script(const std::string& file) {
chai.add(fun<std::string, data, const std::string&>(&data::compress), "compress");
chai.add(fun<std::string, data, const std::string&, int>(&data::compress), "compress");
chai.add(fun(&data::decompress), "decompress");
chai.add(fun(&data::hash), "hash");
// Ensure the love namespace is imported and ready.
chai.import("love");
......
......@@ -4,3 +4,6 @@ assert_not_equal(text, compressed, "love.data.compress()")
var decompressed = love.data.decompress(compressed)
assert_equal(decompressed, text, "love.data.decompress()")
var md5Hash = love.data.hash("md5", "Hello World")
assert_equal(md5Hash, "b10a8db164e0754105b7a99be72e3fe5", "love.data.hash('md5')")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment