Skip to content

Commit

Permalink
docs: added documentation for each subpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed May 28, 2021
1 parent 954381d commit 83ad78b
Show file tree
Hide file tree
Showing 14 changed files with 582 additions and 27 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

[![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) [![Go Reference](https://pkg.go.dev/badge/github.com/aldy505/phc-crypto.svg)](https://pkg.go.dev/github.com/aldy505/phc-crypto) [![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.

Inspired by [Upash](https://github.com/simonepri/upash), also implementing [PHC string format](https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md)

## Usage
Expand Down Expand Up @@ -31,7 +29,10 @@ $ go get github.com/aldy505/phc-crypto
```

```go
import "github.com/aldy505/phc-crypto"
import (
"fmt"
"github.com/aldy505/phc-crypto"
)

func main() {
// Create a crypto instance
Expand Down
93 changes: 93 additions & 0 deletions argon2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# PHC Crypto - Argon2

[![Go Reference](https://pkg.go.dev/badge/github.com/aldy505/phc-crypto.svg)](https://pkg.go.dev/github.com/aldy505/phc-crypto/argon2)

According to [Wikipedia](https://en.wikipedia.org/wiki/Argon2):

Argon2 is a key derivation function that was selected as the winner of the Password Hashing Competition in July 2015. It was designed by Alex Biryukov, Daniel Dinu, and Dmitry Khovratovich from the University of Luxembourg. The reference implementation of Argon2 is released under a Creative Commons CC0 license (i.e. public domain) or the Apache License 2.0, and provides three related versions:

* Argon2d maximizes resistance to GPU cracking attacks. It accesses the memory array in a password dependent order, which reduces the possibility of time–memory trade-off (TMTO) attacks, but introduces possible side-channel attacks.
* Argon2i is optimized to resist side-channel attacks. It accesses the memory array in a password independent order.
* Argon2id is a hybrid version. It follows the Argon2i approach for the first half pass over memory and the Argon2d approach for subsequent passes. The Internet draft recommends using Argon2id except when there are reasons to prefer one of the other two modes.

All three modes allow specification by three parameters that control:

* execution time
* memory required
* degree of parallelism

## Configuration options

| Key | Type | Default | Notes
|---|---|---|---|
| Time | `int` | 32768 | Number of iterations to perform |
| Memory | `int` | 8 | Amount of memory (in kilobytes) to use |
| Parallelism | `int` | 4 | Parallelism factor (threads to run in parallel). |
| KeyLen | `int` | 64 | How many bytes to generate as output. |
| Variant | `string` | id | Argon2 variant to be used (`id` or `i`)|

## Usage with PHC Crypto

```bash
$ go get github.com/aldy505/phc-crypto
```

```go
import (
"fmt"
"github.com/aldy505/phc-crypto"
)

func main() {
crypto, err := phccrypto.Use("argon2", phccrypto.Config{
Parallelism: 3,
Variant: "i",
})
if err != nil {
fmt.Println(err)
}

hash, err := phccrypto.Hash("password")
if err != nil {
fmt.Println(err)
}
fmt.Println(hash) // $argon2i$v=19$m=65536,t=16,p=3$8400b4e5f01f30092b794de34c61a6fdfea6b6b446560fda08a876bd11e9c62e$3fd77927d189...

verify, err := phccrypto.Verify(hash, "password")
if err != nil {
fmt.Println(err)
}
fmt.Println(verify) // true
}
```

## Standalone usage

```bash
$ go get github.com/aldy505/phc-crypto/argon2
```

```go
import (
"fmt"
"github.com/aldy505/phc-crypto/argon2"
)

func main() {

hash, err := argon2.Hash("password", argon2.Config{
Parallelism: 3,
Variant: "i",
})
if err != nil {
fmt.Println(err)
}
fmt.Println(hash) // $argon2i$v=19$m=65536,t=16,p=3$8400b4e5f01f30092b794de34c61a6fdfea6b6b446560fda08a876bd11e9c62e$3fd77927d189...

verify, err := argon2.Verify(hash, "password")
if err != nil {
fmt.Println(err)
}
fmt.Println(verify) // true
}
```
39 changes: 28 additions & 11 deletions argon2/argon2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,53 @@ import (

// Config initialize the config require to create a hash function
type Config struct {
Time uint32
Memory uint32
Parallelism uint8
KeyLen uint32
Time int
Memory int
Parallelism int
KeyLen int
Variant string
}

const (
// Desired number of returned bytes
KEYLEN = 64
// Number of iterations to perform
TIME = 16
// Amount of memory (in kilobytes) to use
MEMORY = 64 * 1024
// Degree of parallelism (i.e. number of threads)
PARALLELISM = 4
// Combines the Argon2d and Argon2i
DEFAULT_VARIANT = "id"
)

// Hash creates a PHC-formatted hash with config provided
func Hash(plain string, config Config) (string, error) {
if config.KeyLen == 0 {
config.KeyLen = 64
config.KeyLen = KEYLEN
}
if config.Time == 0 {
config.Time = 1
config.Time = TIME
}
if config.Memory == 0 {
config.Memory = 64 * 1024
config.Memory = MEMORY
}
if config.Parallelism == 0 {
config.Parallelism = 4
config.Parallelism = PARALLELISM
}
if config.Variant == "" {
config.Variant = "id"
config.Variant = DEFAULT_VARIANT
}

// random-generated salt (16 bytes recommended for password hashing)
salt := make([]byte, 32)
io.ReadFull(rand.Reader, salt)

var hash []byte
if config.Variant == "id" {
hash = argon2.IDKey([]byte(plain), salt, config.Time, config.Memory, config.Parallelism, config.KeyLen)
hash = argon2.IDKey([]byte(plain), salt, uint32(config.Time), uint32(config.Memory), uint8(config.Parallelism), uint32(config.KeyLen))
} else if config.Variant == "i" {
hash = argon2.Key([]byte(plain), salt, config.Time, config.Memory, config.Parallelism, config.KeyLen)
hash = argon2.Key([]byte(plain), salt, uint32(config.Time), uint32(config.Memory), uint8(config.Parallelism), uint32(config.KeyLen))
}
version := argon2.Version
hashString := format.Serialize(format.PHCConfig{
Expand All @@ -62,6 +78,7 @@ func Hash(plain string, config Config) (string, error) {
return hashString, nil
}

// Verify checks the hash if it's equal (by an algorithm) to plain text provided.
func Verify(hash string, plain string) (bool, error) {
deserialize := format.Deserialize(hash)
if !strings.HasPrefix(deserialize.ID, "argon2") {
Expand Down
78 changes: 78 additions & 0 deletions bcrypt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# PHC Crypto - Bcrypt

[![Go Reference](https://pkg.go.dev/badge/github.com/aldy505/phc-crypto.svg)](https://pkg.go.dev/github.com/aldy505/phc-crypto/bcrypt)

According to [Wikipedia](https://en.wikipedia.org/wiki/Bcrypt):

bcrypt is a password-hashing function designed by Niels Provos and David Mazières, based on the Blowfish cipher and presented at USENIX in 1999. Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with increasing computation power.

## Configuration options

| Key | Type | Default | Notes
|---|---|---|---|
| Rounds | `int` | 10 | Cost of rounds, minimum of 4, maximum of 31. |


## Usage with PHC Crypto

```bash
$ go get github.com/aldy505/phc-crypto
```

```go
import (
"fmt"
"github.com/aldy505/phc-crypto"
)

func main() {
crypto, err := phccrypto.Use("bcrypt", phccrypto.Config{
Rounds: 20,
})
if err != nil {
fmt.Println(err)
}

hash, err := phccrypto.Hash("password")
if err != nil {
fmt.Println(err)
}
fmt.Println(hash) // $bcrypt$v=0$r=12$$2432612431322479356256373563666e503557...

verify, err := phccrypto.Verify(hash, "password")
if err != nil {
fmt.Println(err)
}
fmt.Println(verify) // true
}
```

## Standalone usage

```bash
$ go get github.com/aldy505/phc-crypto/bcrypt
```

```go
import (
"fmt"
"github.com/aldy505/phc-crypto/bcrypt"
)

func main() {

hash, err := bcrypt.Hash("password", bcrypt.Config{
Rounds: 12,
})
if err != nil {
fmt.Println(err)
}
fmt.Println(hash) // $bcrypt$v=0$r=12$$2432612431322479356256373563666e503557...

verify, err := bcrypt.Verify(hash, "password")
if err != nil {
fmt.Println(err)
}
fmt.Println(verify) // true
}
```
9 changes: 8 additions & 1 deletion bcrypt/bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ type Config struct {
Rounds int
}

const (
// Cost of rounds, minimum of 4, maximum of 31.
ROUNDS = 10
)

// Hash creates a PHC-formatted hash with config provided
func Hash(plain string, config Config) (string, error) {
if config.Rounds == 0 {
config.Rounds = 10
config.Rounds = ROUNDS
}
hash, err := bcrypt.GenerateFromPassword([]byte(plain), config.Rounds)
hashString := format.Serialize(format.PHCConfig{
Expand All @@ -33,6 +39,7 @@ func Hash(plain string, config Config) (string, error) {
return hashString, nil
}

// Verify checks the hash if it's equal (by an algorithm) to plain text provided.
func Verify(hash string, plain string) (bool, error) {
deserialize := format.Deserialize(hash)
if !strings.HasPrefix(deserialize.ID, "bcrypt") {
Expand Down
Loading

0 comments on commit 83ad78b

Please sign in to comment.