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

Add a bcrypt function #242

Merged
merged 1 commit into from
Dec 2, 2020
Merged
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
17 changes: 11 additions & 6 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"strings"

"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
bcrypt_lib "golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/scrypt"
)

Expand All @@ -49,15 +49,20 @@ func adler32sum(input string) string {
return fmt.Sprintf("%d", hash)
}

func bcrypt(input string) string {
hash, err := bcrypt_lib.GenerateFromPassword([]byte(input), bcrypt_lib.DefaultCost)
if err != nil {
return fmt.Sprintf("failed to encrypt string with bcrypt: %s", err)
}

return string(hash)
}

func htpasswd(username string, password string) string {
if strings.Contains(username, ":") {
return fmt.Sprintf("invalid username: %s", username)
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Sprintf("failed to create htpasswd: %s", err)
}
return fmt.Sprintf("%s:%s", username, hash)
return fmt.Sprintf("%s:%s", username, bcrypt(password))
}

// uuidv4 provides a safe and secure UUID v4 implementation
Expand Down
14 changes: 12 additions & 2 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
bcrypt_lib "golang.org/x/crypto/bcrypt"
)

const (
Expand Down Expand Up @@ -37,6 +37,16 @@ func TestAdler32Sum(t *testing.T) {
}
}

func TestBcrypt(t *testing.T) {
out, err := runRaw(`{{"abc" | bcrypt}}`, nil)
if err != nil {
t.Error(err)
}
if bcrypt_lib.CompareHashAndPassword([]byte(out), []byte("abc")) != nil {
t.Error("Generated hash is not the equivalent for password:", "abc")
}
}

type HtpasswdCred struct {
Username string
Password string
Expand All @@ -59,7 +69,7 @@ func TestHtpasswd(t *testing.T) {
if 0 != strings.Compare(credential.Username, result[0]) && credential.Valid {
t.Error("Generated username did not match for:", credential.Username)
}
if bcrypt.CompareHashAndPassword([]byte(result[1]), []byte(credential.Password)) != nil && credential.Valid {
if bcrypt_lib.CompareHashAndPassword([]byte(result[1]), []byte(credential.Password)) != nil && credential.Valid {
t.Error("Generated hash is not the equivalent for password:", credential.Password)
}
}
Expand Down
7 changes: 7 additions & 0 deletions docs/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ The `adler32sum` function receives a string, and computes its Adler-32 checksum.
```
adler32sum "Hello world!"
```
## bcrypt

The `bcrypt` function receives a string, and generates its `bcrypt` hash.

```
bcrypt "myPassword"
```

## htpasswd

Expand Down
1 change: 1 addition & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ var genericMap = map[string]interface{}{
"concat": concat,

// Crypto:
"bcrypt": bcrypt,
"htpasswd": htpasswd,
"genPrivateKey": generatePrivateKey,
"derivePassword": derivePassword,
Expand Down