aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'internal/protocol/hmac.go')
-rw-r--r--internal/protocol/hmac.go31+31 −0
1 files changed, 31 insertions, 0 deletions
diff --git a/internal/protocol/hmac.go b/internal/protocol/hmac.go
new file mode 100644
--- /dev/null
+++ b/internal/protocol/hmac.go
@@ -0,0 +1,31 @@
+// Copyright (c) 2026 Nikolay Govorov
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package protocol
+
+import (
+ "crypto/hmac"
+ "crypto/rand"
+ "crypto/sha256"
+)
+
+const NonceSize = 32
+
+func GenerateNonce() ([]byte, error) {
+ nonce := make([]byte, NonceSize)
+ _, err := rand.Read(nonce)
+ return nonce, err
+}
+
+// ComputeProof returns HMAC-SHA256(secret, first || second).
+func ComputeProof(secret, first, second []byte) []byte {
+ mac := hmac.New(sha256.New, secret)
+ mac.Write(first)
+ mac.Write(second)
+ return mac.Sum(nil)
+}
+
+func VerifyProof(secret, first, second, proof []byte) bool {
+ expected := ComputeProof(secret, first, second)
+ return hmac.Equal(expected, proof)
+}