summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sha1.cc16
-rw-r--r--src/sha1.hh15
-rw-r--r--src/sha1_md.cc16
-rw-r--r--src/sha1_openssl.cc13
4 files changed, 60 insertions, 0 deletions
diff --git a/src/sha1.cc b/src/sha1.cc
new file mode 100644
index 0000000..5d20ca9
--- /dev/null
+++ b/src/sha1.cc
@@ -0,0 +1,16 @@
+#include "sha1.hh"
+
+namespace sha1 {
+
+std::array<uint8_t, 20> hash(std::span<char const> input) {
+ // std::string and std::string_view -> std::span includes null terminating
+ // byte.
+ if (!input.empty() && input.back() == '\0') {
+ return hash(std::span{reinterpret_cast<uint8_t const*>(input.data()),
+ input.size() - 1});
+ }
+ return hash(
+ std::span{reinterpret_cast<uint8_t const*>(input.data()), input.size()});
+}
+
+} // namespace sha1
diff --git a/src/sha1.hh b/src/sha1.hh
new file mode 100644
index 0000000..a885a09
--- /dev/null
+++ b/src/sha1.hh
@@ -0,0 +1,15 @@
+#ifndef SHA1_HH
+#define SHA1_HH
+
+#include <array> // IWYU pragma: export
+#include <cstdint> // IWYU pragma: export
+#include <span> // IWYU pragma: export
+
+namespace sha1 {
+
+std::array<uint8_t, 20> hash(std::span<uint8_t const> input);
+std::array<uint8_t, 20> hash(std::span<char const> input);
+
+} // namespace sha1
+
+#endif // SHA1_HH
diff --git a/src/sha1_md.cc b/src/sha1_md.cc
new file mode 100644
index 0000000..274eb60
--- /dev/null
+++ b/src/sha1_md.cc
@@ -0,0 +1,16 @@
+#include "sha1.hh"
+
+#include <sha1.h>
+
+namespace sha1 {
+
+std::array<uint8_t, 20> hash(std::span<uint8_t const> input) {
+ std::array<uint8_t, 20> ret;
+ SHA1_CTX ctx;
+ SHA1Init(&ctx);
+ SHA1Update(&ctx, input.data(), input.size());
+ SHA1Final(ret.data(), &ctx);
+ return ret;
+}
+
+} // namespace sha1
diff --git a/src/sha1_openssl.cc b/src/sha1_openssl.cc
new file mode 100644
index 0000000..9e6d77e
--- /dev/null
+++ b/src/sha1_openssl.cc
@@ -0,0 +1,13 @@
+#include "sha1.hh"
+
+#include <openssl/sha.h>
+
+namespace sha1 {
+
+std::array<uint8_t, 20> hash(std::span<uint8_t const> input) {
+ std::array<uint8_t, 20> ret;
+ SHA1(input.data(), input.size(), ret.data());
+ return ret;
+}
+
+} // namespace sha1