Skip to content

Commit e4cd519

Browse files
author
Hasan Demir
committed
Initial commit
0 parents  commit e4cd519

10 files changed

+173
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/hsndmr/go-sanctum
2+
3+
go 1.17

go.sum

Whitespace-only changes.

pkg/crypto/crypto.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package crypto
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
)
7+
8+
// SHA256 Generates the SHA hash of a string
9+
func SHA256(plainText string) string {
10+
hash := sha256.Sum256([]byte(plainText))
11+
return hex.EncodeToString(hash[:])
12+
}

pkg/crypto/crypto_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package crypto
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestGenerateSHA256 is a test for SHA256 function
8+
func TestGenerateSHA256(t *testing.T) {
9+
h := SHA256("test")
10+
if h != "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" {
11+
t.Errorf("Error: %s", "SHA256 does not match")
12+
}
13+
}

pkg/random/random.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package random
2+
3+
import (
4+
"crypto/rand"
5+
"math/big"
6+
)
7+
8+
// GenerateString generates a random string of the specified length
9+
// https://gist.github.com/dopey/c69559607800d2f2f90b1b1ed4e550fb
10+
func GenerateString(size int) (string, error) {
11+
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
12+
ret := make([]byte, size)
13+
for i := 0; i < size; i++ {
14+
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
15+
if err != nil {
16+
return "", err
17+
}
18+
ret[i] = letters[num.Int64()]
19+
}
20+
21+
return string(ret), nil
22+
}

pkg/random/random_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package random
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestGenerateRandomString is a test for Random.Make function
8+
func TestGenerateRandomString(t *testing.T) {
9+
s, err := GenerateString(40)
10+
if err != nil {
11+
t.Errorf("Error: %s", err)
12+
}
13+
14+
if len(s) != 40 {
15+
t.Errorf("Expected length 40, got %d", len(s))
16+
}
17+
}

pkg/token/token.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package token
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
"github.com/hsndmr/go-sanctum/pkg/crypto"
8+
"github.com/hsndmr/go-sanctum/pkg/random"
9+
)
10+
11+
// CreateToken creates a new token
12+
func CreateToken() (*NewToken, error) {
13+
plainText, err := random.GenerateString(40)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
return &NewToken{
19+
plain: plainText,
20+
Hash: crypto.SHA256(plainText),
21+
}, nil
22+
}
23+
24+
// SplitToken returns the hash and ID
25+
func SplitToken(token string) (string, string, error) {
26+
parts := strings.Split(token, "|")
27+
if(len(parts) != 2) {
28+
return "", "", errors.New("invalid token")
29+
}
30+
return parts[0], crypto.SHA256(parts[1]), nil
31+
}
32+
33+
// New Token
34+
type NewToken struct {
35+
plain string
36+
Hash string
37+
}
38+
39+
// GetPlainText returns the plain text
40+
// ModelID.PlainText
41+
func (nt *NewToken) getPlainText(ID string) string {
42+
return ID+"|"+nt.plain
43+
}
44+
45+
46+

pkg/token/token_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package token
2+
3+
import "testing"
4+
5+
// TestCreateToken tests the CreateToken function
6+
func TestCreateToken(t *testing.T) {
7+
nt, err := CreateToken()
8+
if err != nil {
9+
t.Errorf("CreateToken() error: %s", err)
10+
}
11+
12+
if len(nt.Hash) != 64 {
13+
t.Errorf("CreateToken() error: invalid hash length")
14+
}
15+
16+
if len(nt.getPlainText("1")) != 42 {
17+
t.Errorf("CreateToken() error: invalid plain text length")
18+
}
19+
}
20+
21+
func TestSplitToken(t *testing.T) {
22+
nt, _ := CreateToken()
23+
24+
id, hash, err := SplitToken(nt.getPlainText("1"))
25+
26+
if err != nil {
27+
t.Errorf("SplitToken() error: %s", err)
28+
}
29+
30+
if id != "1" {
31+
t.Errorf("SplitToken() error: invalid ID")
32+
}
33+
34+
if hash != nt.Hash {
35+
t.Errorf("SplitToken() error: invalid hash")
36+
}
37+
}

0 commit comments

Comments
 (0)