aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/userkey
diff options
context:
space:
mode:
authorAlexander Rakoczy <[email protected]>2021-10-22 11:47:49 -0400
committerAlexander Rakoczy <[email protected]>2021-10-22 11:47:49 -0400
commitd05245f658c9cb7c66998790d9fdb58a419f971c (patch)
tree3ff56849fecc83452b8964319294b1c202f23c57 /cmd/userkey
parent6128615d04e8ba8abbcfcdaf449451b8d68892ca (diff)
upload
Diffstat (limited to 'cmd/userkey')
-rw-r--r--cmd/userkey/main.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/cmd/userkey/main.go b/cmd/userkey/main.go
new file mode 100644
index 0000000..5ecd5f1
--- /dev/null
+++ b/cmd/userkey/main.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "crypto/hmac"
+ "crypto/sha256"
+ "encoding/hex"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+)
+
+func main() {
+ user := os.Args[1]
+ if len(user) < 3 {
+ log.Fatalf("User name argument expected.")
+ }
+ encoded, err := ioutil.ReadAll(os.Stdin)
+ if err != nil || len(encoded) == 0 {
+ log.Fatalf("Expected exactly 1 secret")
+ }
+ sk, err := hex.DecodeString(string(encoded))
+ if err != nil || len(sk) < 32 {
+ log.Fatalf("hex.DecodeString() = %d, %v", len(sk), err)
+ return
+ }
+ mac := hmac.New(sha256.New, sk)
+ mac.Write([]byte(user))
+ fmt.Println(hex.EncodeToString(mac.Sum(nil)))
+}
+
+// ValidMAC reports whether messageMAC is a valid HMAC tag for message.
+func ValidMAC(message, messageMAC, key []byte) bool {
+ mac := hmac.New(sha256.New, key)
+ mac.Write(message)
+ expectedMAC := mac.Sum(nil)
+ return hmac.Equal(messageMAC, expectedMAC)
+}
+
+var sk []byte