Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
slim-sprig
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CodeLinaro
yocto-mirrors
github
go-task
slim-sprig
Commits
ed89b5bf
Unverified
Commit
ed89b5bf
authored
4 years ago
by
Matt Farina
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #242 from robbiet480/bcrypt
Add a bcrypt function
parents
1bbf15f0
c0f62708
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
crypto.go
+11
-6
11 additions, 6 deletions
crypto.go
crypto_test.go
+12
-2
12 additions, 2 deletions
crypto_test.go
docs/crypto.md
+7
-0
7 additions, 0 deletions
docs/crypto.md
functions.go
+1
-0
1 addition, 0 deletions
functions.go
with
31 additions
and
8 deletions
crypto.go
+
11
−
6
View file @
ed89b5bf
...
...
@@ -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"
)
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
crypto_test.go
+
12
−
2
View file @
ed89b5bf
...
...
@@ -9,7 +9,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
bcrypt_lib
"golang.org/x/crypto/bcrypt"
)
const
(
...
...
@@ -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
...
...
@@ -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
)
}
}
...
...
This diff is collapsed.
Click to expand it.
docs/crypto.md
+
7
−
0
View file @
ed89b5bf
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
functions.go
+
1
−
0
View file @
ed89b5bf
...
...
@@ -303,6 +303,7 @@ var genericMap = map[string]interface{}{
"concat"
:
concat
,
// Crypto:
"bcrypt"
:
bcrypt
,
"htpasswd"
:
htpasswd
,
"genPrivateKey"
:
generatePrivateKey
,
"derivePassword"
:
derivePassword
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment