Initial commit

This commit is contained in:
euphoria_laxis 2022-12-04 02:18:05 +01:00
commit 33b0953b07
8 changed files with 236 additions and 0 deletions

20
.gitignore vendored Normal file
View File

@ -0,0 +1,20 @@
### JetBrains
.idea/
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/

8
LICENSE Normal file
View File

@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright © 2022 Euphoria Laxis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

38
README.md Normal file
View File

@ -0,0 +1,38 @@
# Argon2 utils
## About
Utils to encrypt passwords using argon2
## Usage
### Example
````go
func func main() {
password := 'qwerty@123'
hashedString, err := argon2_utils.HashStringArgon2(password)
if err != nil {
...
}
match, err := argon2_utils.CompareStringToArgon2Hash(randomString, hashedString)
if err != nil {
...
}
if !match {
log.Println("passwords don't match")
} else {
log.Println("passwords match")
}
}
````
This package also contains a **RandomString(int)(string,error)** function.
## Contributions
**Euphoria Laxis**
## License
This project is under [MIT License](./LICENSE)

53
_tests/crypto_test.go Normal file
View File

@ -0,0 +1,53 @@
package _tests
import (
"argon2_utils"
"log"
"testing"
)
var (
randomString, hashedString string
)
func TestHashStringArgon2(t *testing.T) {
var err error
randomString, err = argon2_utils.RandomString(32)
if err != nil {
log.Print(err)
t.Fail()
}
hashedString, err = argon2_utils.HashStringArgon2(randomString)
if err != nil {
log.Print(err)
t.Fail()
}
}
func TestCompareStringToArgon2Hash(t *testing.T) {
match, err := argon2_utils.CompareStringToArgon2Hash(randomString, hashedString)
if err != nil {
log.Print(err)
t.Fail()
}
if !match {
log.Println("passwords comparison failed")
log.Println("passwords should match")
t.Fail()
}
randomString, err = argon2_utils.RandomString(32)
if err != nil {
log.Print(err)
t.Fail()
}
match, err = argon2_utils.CompareStringToArgon2Hash(randomString, hashedString)
if err != nil {
log.Print(err)
t.Fail()
}
if match {
log.Println("passwords comparison failed")
log.Println("passwords shouldn't match")
t.Fail()
}
}

83
crypto.go Normal file
View File

@ -0,0 +1,83 @@
package argon2_utils
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"fmt"
"golang.org/x/crypto/argon2"
"strings"
)
func generateRandomBytes(n uint32) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func decodeHash(encodedHash string) (p *params, salt, hash []byte, err error) {
vals := strings.Split(encodedHash, "$")
if len(vals) != 6 {
return nil, nil, nil, ErrInvalidHash
}
var version int
_, err = fmt.Sscanf(vals[2], "v=%d", &version)
if err != nil {
return nil, nil, nil, err
}
if version != argon2.Version {
return nil, nil, nil, ErrIncompatibleVersion
}
p = &params{}
_, err = fmt.Sscanf(vals[3], "m=%d,t=%d,p=%d", &p.memory, &p.iterations, &p.parallelism)
if err != nil {
return nil, nil, nil, err
}
salt, err = base64.RawStdEncoding.DecodeString(vals[4])
if err != nil {
return nil, nil, nil, err
}
p.saltLength = uint32(len(salt))
hash, err = base64.RawStdEncoding.DecodeString(vals[5])
if err != nil {
return nil, nil, nil, err
}
p.keyLength = uint32(len(hash))
return p, salt, hash, nil
}
func CompareStringToArgon2Hash(password string, hashedPassword string) (match bool, err error) {
p, salt, hash, err := decodeHash(hashedPassword)
if err != nil {
return false, err
}
otherHash := argon2.IDKey([]byte(password), salt, p.iterations, p.memory, p.parallelism, p.keyLength)
if subtle.ConstantTimeCompare(hash, otherHash) == 1 {
return true, nil
}
return false, nil
}
func HashStringArgon2(password string) (encodedHash string, err error) {
salt, err := generateRandomBytes(p.saltLength)
if err != nil {
return "", err
}
hash := argon2.IDKey([]byte(password), salt, p.iterations, p.memory, p.parallelism, p.keyLength)
b64Salt := base64.RawStdEncoding.EncodeToString(salt)
b64Hash := base64.RawStdEncoding.EncodeToString(hash)
encodedHash = fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", argon2.Version, p.memory, p.iterations, p.parallelism, b64Salt, b64Hash)
return encodedHash, nil
}
func RandomString(s int) (string, error) {
b, err := generateRandomBytes(uint32(s))
return base64.URLEncoding.EncodeToString(b), err
}

23
crypto_struct.go Normal file
View File

@ -0,0 +1,23 @@
package argon2_utils
import "errors"
type params struct {
memory uint32
iterations uint32
parallelism uint8
saltLength uint32
keyLength uint32
}
var (
ErrInvalidHash = errors.New("the encoded hash is not in the correct format")
ErrIncompatibleVersion = errors.New("incompatible version of argon2")
p = &params{
memory: 64 * 1024,
iterations: 3,
parallelism: 2,
saltLength: 16,
keyLength: 32,
}
)

7
go.mod Normal file
View File

@ -0,0 +1,7 @@
module argon2_utils
go 1.19
require golang.org/x/crypto v0.3.0
require golang.org/x/sys v0.2.0 // indirect

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=