Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(crypto): add uuidv5 #415

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ func uuidv4() string {
return uuid.New().String()
}

// uuidv5 provides a safe and secure UUID v5 implementation
func uuidv5(namespace, name string) string {
// The namespace is always a UUID
ns, err := uuid.Parse(namespace)
if err != nil {
return fmt.Sprintf("namespace must be a valid UUID")
}

return uuid.NewSHA1(ns, []byte(name)).String()
}

var masterPasswordSeed = "com.lyndir.masterpassword"

var passwordTypeTemplates = map[string][][]byte{
Expand Down
28 changes: 27 additions & 1 deletion crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestRandBytes(t *testing.T) {
}
}

func TestUUIDGeneration(t *testing.T) {
func TestUUIDv4Generation(t *testing.T) {
tpl := `{{uuidv4}}`
out, err := runRaw(tpl, nil)
if err != nil {
Expand All @@ -222,6 +222,32 @@ func TestUUIDGeneration(t *testing.T) {
}
}

func TestUUIDv5Generation(t *testing.T) {
tpl := `{{uuidv5 "2be4f575-0625-4376-bfca-fc237ac4fd8a" "Hello World"}}`
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}

if len(out) != 36 {
t.Error("Expected UUID of length 36")
}

out2, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}

if out != out2 {
t.Error("Expected subsequent UUID generations to be identical")
}

tpl = `{{uuidv5 "2be4f575-0625-4376-bfca-fc237ac4fd8a" "Hello World"}}`
if err := runt(tpl, "c493a152-08ba-5679-a958-de98ebcc8160"); err != nil {
t.Error(err)
}
}

func TestBuildCustomCert(t *testing.T) {
ca, _ := generateCertificateAuthority("example.com", 365)
tpl := fmt.Sprintf(
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The Sprig library provides over 70 template functions for Go's template language
- [Path and Filepath Functions](paths.md): `base`, `dir`, `ext`, `clean`, `isAbs`, `osBase`, `osDir`, `osExt`, `osClean`, `osIsAbs`
- [Flow Control Functions](flow_control.md): `fail`
- Advanced Functions
- [UUID Functions](uuid.md): `uuidv4`
- [UUID Functions](uuid.md): `uuidv4`, `uuidv5`
- [OS Functions](os.md): `env`, `expandenv`
- [Version Comparison Functions](semver.md): `semver`, `semverCompare`
- [Reflection](reflection.md): `typeOf`, `kindIs`, `typeIsLike`, etc.
Expand Down
13 changes: 13 additions & 0 deletions docs/uuid.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# UUID Functions

## UUID v4

Sprig can generate UUID v4 universally unique IDs.

```
uuidv4
```

The above returns a new UUID of the v4 (randomly generated) type.

## UUID v5

Sprig can generate UUID v5 which can output deterministic IDs for a given namespace and name.

```
uuidv5 "2be4f575-0625-4376-bfca-fc237ac4fd8a" "Hello World"
```

The above returns a new deterministic UUID that will always be the same as long as the parameters stay identical.
The output here would be `c493a152-08ba-5679-a958-de98ebcc8160`
1 change: 1 addition & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ var genericMap = map[string]interface{}{

// UUIDs:
"uuidv4": uuidv4,
"uuidv5": uuidv5,

// SemVer:
"semver": semver,
Expand Down