Skip to content

Commit

Permalink
change crypto API
Browse files Browse the repository at this point in the history
add support go modules
add 1.10-1.13 go in travis
  • Loading branch information
Jan Seidl committed Nov 13, 2019
1 parent 70131fd commit d77c767
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 70 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go:
- "1.10"
- 1.11
- 1.12
- 1.13

install:
- make setup
Expand Down
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
[[constraint]]
name = "github.com/mitchellh/go-homedir"
version = "1.0.0"

[[constraint]]
name = "github.com/JaSei/hashutil-go"
version = "2.0.0"
92 changes: 66 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,6 @@ functions which isn't in core libraries (like `Copy` for example)

## Usage

#### type CryptoHash

```go
type CryptoHash struct {
hash.Hash
}
```


#### func (*CryptoHash) BinSum

```go
func (hash *CryptoHash) BinSum() []byte
```
BinSum method is like hash.Sum(nil)

#### func (*CryptoHash) HexSum

```go
func (hash *CryptoHash) HexSum() string
```
HexSum method retun hexstring representation of hash.Sum

#### type LinesFunc

```go
Expand Down Expand Up @@ -111,7 +88,11 @@ type Path interface {

CopyFrom(io.Reader) (int64, error)

Crypto(crypto.Hash) (*CryptoHash, error)
CryptoMd5() (hashutil.Md5, error)
CryptoSha1() (hashutil.Sha1, error)
CryptoSha256() (hashutil.Sha256, error)
CryptoSha384() (hashutil.Sha384, error)
CryptoSha512() (hashutil.Sha512, error)

MakePath() error
MakePathMode(os.FileMode) error
Expand Down Expand Up @@ -275,11 +256,70 @@ func (path PathImpl) CopyFrom(reader io.Reader) (copyied int64, err error)
```
CopyFrom copy stream from reader to path (file after copy close and sync)

#### func (PathImpl) Crypto
#### func (PathImpl) CryptoMd5

```go
func (path PathImpl) CryptoMd5() (hashutil.Md5, error)
```
CryptoMd5 method access hash funcionality like Path::Tiny Digest return
[hashutil.Md5](github.com/JaSei/hashutil-go) type

for example print of Md5 hexstring

hash, err := path.CryptoMd5()
fmt.Println(hash.String())

#### func (PathImpl) CryptoSha1

```go
func (path PathImpl) Crypto(hash crypto.Hash) (c *CryptoHash, err error)
func (path PathImpl) CryptoSha1() (hashutil.Sha1, error)
```
CryptoSha1 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha1](github.com/JaSei/hashutil-go) type

for example print of Sha1 hexstring

hash, err := path.CryptoSha1()
fmt.Println(hash.String())

#### func (PathImpl) CryptoSha256

```go
func (path PathImpl) CryptoSha256() (hashutil.Sha256, error)
```
CryptoSha256 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha256](github.com/JaSei/hashutil-go) type

for example print of Sha256 hexstring

hash, err := path.CryptoSha256()
fmt.Println(hash.String())

#### func (PathImpl) CryptoSha384

```go
func (path PathImpl) CryptoSha384() (hashutil.Sha384, error)
```
CryptoSha384 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha384](github.com/JaSei/hashutil-go) type

for example print of Sha284 hexstring

hash, err := path.CryptoSha284()
fmt.Println(hash.String())

#### func (PathImpl) CryptoSha512

```go
func (path PathImpl) CryptoSha512() (hashutil.Sha512, error)
```
CryptoSha512 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha512](github.com/JaSei/hashutil-go) type

for example print of Sha512 hexstring

hash, err := path.CryptoSha512()
fmt.Println(hash.String())

#### func (PathImpl) Exists

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.1
1.0.0
107 changes: 80 additions & 27 deletions crypto.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,92 @@
package pathutil

import (
"crypto"
"fmt"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"io"

"github.com/JaSei/hashutil-go"
)

// Crypto method access hash funcionality like Path::Tiny Digest
// look to https://godoc.org/crypto#Hash for list possible crypto hash functions
// CryptoMd5 method access hash funcionality like Path::Tiny Digest
// return [hashutil.Md5](github.com/JaSei/hashutil-go) type
//
// for example print of Md5 hexstring
// hash, err := path.CryptoMd5()
// fmt.Println(hash.String())
func (path PathImpl) CryptoMd5() (hashutil.Md5, error) {
c, err := path.crypto(md5.New())
if err != nil {
return hashutil.Md5{}, nil
}

return hashutil.HashToMd5(c)
}

// CryptoSha1 method access hash funcionality like Path::Tiny Digest
// return [hashutil.Sha1](github.com/JaSei/hashutil-go) type
//
// for example print of Sha1 hexstring
// hash, err := path.CryptoSha1()
// fmt.Println(hash.String())
func (path PathImpl) CryptoSha1() (hashutil.Sha1, error) {
c, err := path.crypto(sha1.New())
if err != nil {
return hashutil.Sha1{}, nil
}

return hashutil.HashToSha1(c)
}

// CryptoSha256 method access hash funcionality like Path::Tiny Digest
// return [hashutil.Sha256](github.com/JaSei/hashutil-go) type
//
// for example print of Sha256 hexstring
// hash, err := path.Crypto(crypto.SHA256)
// fmt.Println(hash.HexSum())
// hash, err := path.CryptoSha256()
// fmt.Println(hash.String())
func (path PathImpl) CryptoSha256() (hashutil.Sha256, error) {
c, err := path.crypto(sha256.New())
if err != nil {
return hashutil.Sha256{}, nil
}

func (path PathImpl) Crypto(hash crypto.Hash) (c *CryptoHash, err error) {
return hashutil.HashToSha256(c)
}

// CryptoSha384 method access hash funcionality like Path::Tiny Digest
// return [hashutil.Sha384](github.com/JaSei/hashutil-go) type
//
// for example print of Sha284 hexstring
// hash, err := path.CryptoSha284()
// fmt.Println(hash.String())
func (path PathImpl) CryptoSha384() (hashutil.Sha384, error) {
c, err := path.crypto(sha512.New384())
if err != nil {
return hashutil.Sha384{}, nil
}

return hashutil.HashToSha384(c)
}

// CryptoSha512 method access hash funcionality like Path::Tiny Digest
// return [hashutil.Sha512](github.com/JaSei/hashutil-go) type
//
// for example print of Sha512 hexstring
// hash, err := path.CryptoSha512()
// fmt.Println(hash.String())
func (path PathImpl) CryptoSha512() (hashutil.Sha512, error) {
c, err := path.crypto(sha512.New())
if err != nil {
return hashutil.Sha512{}, nil
}

return hashutil.HashToSha512(c)
}

func (path PathImpl) crypto(h hash.Hash) (ret hash.Hash, err error) {
reader, err := path.OpenReader()
if err != nil {
return nil, err
Expand All @@ -25,30 +97,11 @@ func (path PathImpl) Crypto(hash crypto.Hash) (c *CryptoHash, err error) {
}
}()

h := hash.New()

_, err = io.Copy(h, reader)

if err != nil {
return nil, err
}

return &CryptoHash{h}, nil
}

// CryptoHash struct is only abstract for hash.Hash interface
// for possible use with methods

type CryptoHash struct {
hash.Hash
}

// BinSum method is like hash.Sum(nil)
func (hash *CryptoHash) BinSum() []byte {
return hash.Sum(nil)
}

// HexSum method retun hexstring representation of hash.Sum
func (hash *CryptoHash) HexSum() string {
return fmt.Sprintf("%x", hash.Sum(nil))
return h, nil
}
26 changes: 18 additions & 8 deletions crypto_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package pathutil

import (
"crypto"
"testing"

"github.com/JaSei/hashutil-go"
"github.com/stretchr/testify/assert"
)

const emptyFileSha256Hex = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

var emptyFileSha256Bin = []uint8{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}

func TestPathCrypto(t *testing.T) {
path, err := NewTempFile(TempOpt{})
assert.Nil(t, err)
defer func() {
assert.NoError(t, path.Remove())
}()

hash, err := path.Crypto(crypto.Hash(crypto.SHA256))
md5Hash, err := path.CryptoMd5()
assert.NoError(t, err)
assert.True(t, hashutil.EmptyMd5().Equal(md5Hash))

assert.Equal(t, emptyFileSha256Hex, hash.HexSum())
assert.Equal(t, emptyFileSha256Bin, hash.BinSum())
sha1Hash, err := path.CryptoSha1()
assert.NoError(t, err)
assert.True(t, hashutil.EmptySha1().Equal(sha1Hash))

sha256Hash, err := path.CryptoSha256()
assert.NoError(t, err)
assert.True(t, hashutil.EmptySha256().Equal(sha256Hash))

sha384Hash, err := path.CryptoSha384()
assert.NoError(t, err)
assert.True(t, hashutil.EmptySha384().Equal(sha384Hash))

sha512Hash, err := path.CryptoSha512()
assert.NoError(t, err)
assert.True(t, hashutil.EmptySha512().Equal(sha512Hash))
}
7 changes: 5 additions & 2 deletions examples/download_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package pathutil_test

import (
"fmt"
"net/http"
"testing"

"github.com/JaSei/pathutil-go"
"github.com/stretchr/testify/assert"
)

const sizeOfExampleSite = 1256

func TestDownload(t *testing.T) {
response, err := http.Get("http://example.com")

Expand All @@ -33,11 +36,11 @@ func TestDownload(t *testing.T) {
t.Log(err)
}

assert.Equal(t, int64(1270), copyied, "Copy 1270 bytes")
assert.Equal(t, int64(sizeOfExampleSite), copyied, fmt.Sprintf("Copy %d bytes", sizeOfExampleSite))

ctx, err := temp.Slurp()

assert.Nil(t, err)

assert.Equal(t, 1270, len(ctx), "Size of http://example.com are 1270")
assert.Equal(t, sizeOfExampleSite, len(ctx), fmt.Sprintf("Size of http://example.com are %d", sizeOfExampleSite))
}
5 changes: 2 additions & 3 deletions examples/recursive_sha256sum_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pathutil_test

import (
"crypto"
"fmt"
"testing"

Expand All @@ -19,10 +18,10 @@ func TestVisitRecursiveAndHashAllFiles(t *testing.T) {
return
}

hash, err := path.Crypto(crypto.SHA256)
hash, err := path.CryptoSha256()

if err == nil {
fmt.Printf("%s\t%s\n", hash.HexSum(), path.String())
fmt.Printf("%s\t%s\n", hash, path)
} else {
fmt.Println(err)
}
Expand Down
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/JaSei/pathutil-go

go 1.13

require (
github.com/JaSei/hashutil-go v0.0.0-20191113002909-3ff95654e7b7
github.com/davecgh/go-spew v1.1.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.4.0
)
Loading

0 comments on commit d77c767

Please sign in to comment.