Skip to content

Commit

Permalink
style: codacy formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed May 26, 2021
1 parent 479b6d5 commit 2c500bc
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PHC Crypto

[![GitHub release (latest SemVer including pre-releases)](https://img.shields.io/github/v/release/aldy505/phc-crypto?include_prereleases)](https://github.com/aldy505/phc-crypto/releases) [![GitHub](https://img.shields.io/github/license/aldy505/phc-crypto)](https://github.com/aldy505/phc-crypto/blob/master/LICENSE) [![Codecov](https://img.shields.io/codecov/c/gh/aldy505/phc-crypto)](https://www.codecov.io/gh/aldy505/phc-crypto) [![CodeFactor](https://www.codefactor.io/repository/github/aldy505/phc-crypto/badge)](https://www.codefactor.io/repository/github/aldy505/phc-crypto) [![Build test](https://github.com/aldy505/phc-crypto/actions/workflows/build.yml/badge.svg)](https://github.com/aldy505/phc-crypto/actions/workflows/build.yml) [![Build test](https://github.com/aldy505/phc-crypto/actions/workflows/coverage.yml/badge.svg)](https://github.com/aldy505/phc-crypto/actions/workflows/coverage.yml)
[![GitHub release (latest SemVer including pre-releases)](https://img.shields.io/github/v/release/aldy505/phc-crypto?include_prereleases)](https://github.com/aldy505/phc-crypto/releases) [![GitHub](https://img.shields.io/github/license/aldy505/phc-crypto)](https://github.com/aldy505/phc-crypto/blob/master/LICENSE) [![codecov](https://codecov.io/gh/aldy505/phc-crypto/branch/master/graph/badge.svg?token=HUTQURBZ73)](https://codecov.io/gh/aldy505/phc-crypto) [![CodeFactor](https://www.codefactor.io/repository/github/aldy505/phc-crypto/badge)](https://www.codefactor.io/repository/github/aldy505/phc-crypto) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/16c40f49aabe4e89afea7c1e1d90a483)](https://www.codacy.com/gh/aldy505/phc-crypto/dashboard?utm_source=github.com&utm_medium=referral&utm_content=aldy505/phc-crypto&utm_campaign=Badge_Grade) [![Build test](https://github.com/aldy505/phc-crypto/actions/workflows/build.yml/badge.svg)](https://github.com/aldy505/phc-crypto/actions/workflows/build.yml) [![Build test](https://github.com/aldy505/phc-crypto/actions/workflows/coverage.yml/badge.svg)](https://github.com/aldy505/phc-crypto/actions/workflows/coverage.yml)

A work in progress.

Expand Down Expand Up @@ -47,7 +47,7 @@ func main() {
* Argon2 (argon2i & argon2id)
* Chacha20poly1305

## Want to contribute?
## Contribute

Yes please! I'm still new to Go and I create this module (or package if you will) to help me fulfill a need on my project. Feel free to refactor, add new feature, fix unknown bugs, and have fun!

Expand Down
9 changes: 5 additions & 4 deletions argon2/argon2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/crypto/argon2"
)

// Config initialize the config require to create a hash function
type Config struct {
Time uint32
Memory uint32
Expand Down Expand Up @@ -48,7 +49,7 @@ func Hash(plain string, config Config) (string, error) {
}
version := argon2.Version
hashString := format.Serialize(format.PHCConfig{
Id: "argon2" + config.Variant,
ID: "argon2" + config.Variant,
Version: version,
Params: map[string]interface{}{
"m": strconv.Itoa(int(config.Memory)),
Expand All @@ -63,7 +64,7 @@ func Hash(plain string, config Config) (string, error) {

func Verify(hash string, plain string) (bool, error) {
deserialize := format.Deserialize(hash)
if strings.HasPrefix(deserialize.Id, "argon2") == false {
if !strings.HasPrefix(deserialize.ID, "argon2") {
return false, errors.New("hashed string is not argon instance")
}

Expand All @@ -87,9 +88,9 @@ func Verify(hash string, plain string) (bool, error) {
return false, err
}

if deserialize.Id == "argon2id" {
if deserialize.ID == "argon2id" {
verifyHash = argon2.IDKey([]byte(plain), []byte(deserialize.Salt), uint32(time), uint32(memory), uint8(parallelism), keyLen)
} else if deserialize.Id == "argon2i" {
} else if deserialize.ID == "argon2i" {
verifyHash = argon2.Key([]byte(plain), []byte(deserialize.Salt), uint32(time), uint32(memory), uint8(parallelism), keyLen)
}

Expand Down
11 changes: 5 additions & 6 deletions bcrypt/bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"golang.org/x/crypto/bcrypt"
)

// Config initialize the config require to create a hash function
type Config struct {
Rounds int
}
Expand All @@ -19,7 +20,7 @@ func Hash(plain string, config Config) (string, error) {
}
hash, err := bcrypt.GenerateFromPassword([]byte(plain), config.Rounds)
hashString := format.Serialize(format.PHCConfig{
Id: "bcrypt",
ID: "bcrypt",
Version: 0,
Params: map[string]interface{}{
"r": config.Rounds,
Expand All @@ -28,14 +29,13 @@ func Hash(plain string, config Config) (string, error) {
})
if err != nil {
return "", err
} else {
return hashString, nil
}
return hashString, nil
}

func Verify(hash string, plain string) (bool, error) {
deserialize := format.Deserialize(hash)
if !strings.HasPrefix(deserialize.Id, "bcrypt") {
if !strings.HasPrefix(deserialize.ID, "bcrypt") {
return false, errors.New("hashed string is not a bcrypt instance")
}
decodedHash, err := hex.DecodeString(deserialize.Hash)
Expand All @@ -45,7 +45,6 @@ func Verify(hash string, plain string) (bool, error) {
err = bcrypt.CompareHashAndPassword(decodedHash, []byte(plain))
if err != nil {
return false, nil
} else {
return true, nil
}
return true, nil
}
2 changes: 1 addition & 1 deletion chacha20poly1305/chacha20poly1305.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Hash(plain string) (string, error) {
Params: map[string]interface{}{
"n": hex.EncodeToString(nonce),
},
Id: "chacha20poly1305",
ID: "chacha20poly1305",
})
return hashString, nil
}
10 changes: 7 additions & 3 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import (
"strings"
)

// PHCConfig is a struct required for creating a PHC string
type PHCConfig struct {
Id string
ID string
Version int
Params map[string]interface{}
Salt string
Hash string
}

// Serialize converts PHCConfig struct into a PHC string.
// See https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md
func Serialize(config PHCConfig) string {
var params []string
for key, value := range config.Params {
params = append(params, key+"="+strconv.Itoa(value.(int)))
}
return "$" + config.Id + "$v=" + strconv.Itoa(config.Version) + "$" + strings.Join(params, ",") + "$" + config.Salt + "$" + config.Hash
return "$" + config.ID + "$v=" + strconv.Itoa(config.Version) + "$" + strings.Join(params, ",") + "$" + config.Salt + "$" + config.Hash
}

// Deserialize converts a PHC string into a PHCConfig struct
func Deserialize(hash string) PHCConfig {
hashArray := strings.Split(hash, "$")
params := make(map[string]interface{})
Expand All @@ -32,7 +36,7 @@ func Deserialize(hash string) PHCConfig {
}
version, _ := strconv.Atoi(hashArray[2])
return PHCConfig{
Id: hashArray[1],
ID: hashArray[1],
Version: version,
Params: params,
Salt: hashArray[4],
Expand Down
3 changes: 2 additions & 1 deletion pbkdf2/pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/crypto/pbkdf2"
)

// Config initialize the config require to create a hash function
type Config struct {
Rounds int
KeyLen int
Expand Down Expand Up @@ -53,7 +54,7 @@ func Hash(plain string, config Config) (string, error) {
}

hashString := format.Serialize(format.PHCConfig{
Id: "pbkdf2" + config.HashFunc,
ID: "pbkdf2" + config.HashFunc,
Params: map[string]interface{}{
"i": config.Rounds,
},
Expand Down
12 changes: 8 additions & 4 deletions phc-crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"github.com/aldy505/phc-crypto/scrypt"
)

// Algo returns struct that will be use on Hash and Verify function
type Algo struct {
Name string
Config *Config
}

// Config returns the general config of the hashing function
type Config struct {
Cost int
Rounds int
Expand All @@ -24,6 +26,7 @@ type Config struct {
HashFunc string
}

// Use initiates the hash/verify function
func Use(name string, config Config) (*Algo, error) {
var algo *Algo

Expand All @@ -41,6 +44,7 @@ func Use(name string, config Config) (*Algo, error) {
}
}

// Hash returns a PHC formatted string of a hash function (that was initiated from Use)
func (a *Algo) Hash(plain string) (hash string, err error) {
if a.Name == "scrypt" {
hash, err = scrypt.Hash(plain, scrypt.Config{
Expand Down Expand Up @@ -74,13 +78,13 @@ func (a *Algo) Hash(plain string) (hash string, err error) {
} else if a.Name == "chacha20poly1305" {
hash, err = chacha20poly1305.Hash(plain)
return
} else {
hash = ""
err = errors.New("the algorithm provided is not supported")
return
}
hash = ""
err = errors.New("the algorithm provided is not supported")
return
}

// Hash returns a boolean of a hash function (that was initiated from Use)
func (a *Algo) Verify(hash, plain string) (bool, error) {
return false, nil
}
Expand Down
5 changes: 3 additions & 2 deletions scrypt/scrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/crypto/scrypt"
)

// Config initialize the config require to create a hash function
type Config struct {
Cost int
Rounds int
Expand Down Expand Up @@ -43,7 +44,7 @@ func Hash(plain string, config Config) (string, error) {
}

hashString := format.Serialize(format.PHCConfig{
Id: "scrypt",
ID: "scrypt",
Version: 0,
Params: map[string]interface{}{
"ln": config.Cost,
Expand All @@ -59,7 +60,7 @@ func Hash(plain string, config Config) (string, error) {

func Verify(hash string, plain string) (bool, error) {
deserialize := format.Deserialize(hash)
if !strings.HasPrefix(deserialize.Id, "scrypt") {
if !strings.HasPrefix(deserialize.ID, "scrypt") {
return false, errors.New("hashed string is not scrypt instance")
}

Expand Down

0 comments on commit 2c500bc

Please sign in to comment.