Skip to content

Commit f3800e1

Browse files
committed
basic cryptographic checks
1 parent 12cbf5c commit f3800e1

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

internal/cryptography/checksum.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cryptography
2+
3+
import (
4+
"crypto/sha256"
5+
"fmt"
6+
)
7+
8+
func CalculateChecksum(data []byte) string {
9+
sum := sha256.Sum224(data)
10+
11+
return fmt.Sprintf("%x", sum)
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cryptography
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestSum224(t *testing.T) {
9+
msg := "ABCD"
10+
res := CalculateChecksum([]byte(msg))
11+
12+
if !strings.HasSuffix(res, "60b6") {
13+
t.Error("Expected input to generate another checksum...")
14+
}
15+
}

internal/txpfiber/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import (
88

99
"github.com/charmbracelet/log"
1010
"github.com/gofiber/fiber/v2"
11+
"github.com/unhanded/txp/internal/cryptography"
1112
)
1213

1314
func placeContext(c *fiber.Ctx, targetDir string) error {
15+
b := c.Body()
16+
sum := cryptography.CalculateChecksum(b)
1417
ctx := map[string]any{
1518
"fromIP": c.IP(),
1619
"timestamp": time.Now().Format("2006-01-02 15:04:05 UTC"),
1720
"method": c.Method(),
21+
"sha224": sum,
1822
}
1923

2024
b, jsonErr := json.Marshal(ctx)

0 commit comments

Comments
 (0)