aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2023-06-13 09:57:45 -0400
committerXe Iaso <me@xeiaso.net>2023-06-13 09:57:45 -0400
commitbc09dfec77d987b40580d5f2941b5178004b8e19 (patch)
tree1fba575fb043b12922ba3c6b8e6ee3ef33bd7cf8
parent5a77317341d549d3ee41e2ac4371c931fbf704e0 (diff)
downloadx-bc09dfec77d987b40580d5f2941b5178004b8e19.tar.xz
x-bc09dfec77d987b40580d5f2941b5178004b8e19.zip
internal: add a cryptography primitives thing
Signed-off-by: Xe Iaso <me@xeiaso.net>
-rw-r--r--internal/crypto.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/internal/crypto.go b/internal/crypto.go
new file mode 100644
index 0000000..37fae9e
--- /dev/null
+++ b/internal/crypto.go
@@ -0,0 +1,42 @@
+package internal
+
+import (
+ "crypto/md5"
+ "fmt"
+ "hash/fnv"
+)
+
+// Hash is a simple wrapper around the MD5 algorithm implementation in the
+// Go standard library. It takes in data and a salt and returns the hashed
+// representation.
+func Hash(data string, salt string) string {
+ output := md5.Sum([]byte(data + salt))
+ return fmt.Sprintf("%x", output)
+}
+
+// Fnv is a non-cryptographic hashing function based on non-cryptographic hash
+// functions created by Glenn Fowler, Landon Curt Noll, and Phong Vo. This
+// wraps the standard library FNV hash function by representing it as a 32 bit
+// integer. Then it takes that number and "hashes" it by adding the rune's
+// unicode value (as a uint 32) and then takes the modulus of 26, letting the number
+// be represented as a letter of the alphabet. This is not a cryptographically
+// secure operation, it is purely to replace numbers with a human-readable string
+// to satisfy the requirement that any vhost with a "/" in it cannot end in a number
+// (to avoid someone obtaining a vhost that is a cidr mask, it can cause issues).
+func Fnv(data string) string {
+ h := fnv.New32()
+ h.Write([]byte(data))
+
+ hash := h.Sum32()
+
+ alphabet := "abcdefghijklmnopqrstuvwxyz"
+ res := ""
+
+ for _, char := range fmt.Sprintf("%d", hash) {
+ res = res + string(alphabet[(uint32(char)+hash)%26])
+
+ hash = (hash << 1) | (hash >> 31) // "rotate" the number for extra variance
+ }
+
+ return res
+}