Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Nov 11, 2024
1 parent cda42ba commit bec4fa1
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 132 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

*??? ??, ????*

### CHANGES

- `pkg/crypto/hashing`: doOnce encoding.HexEncode -> ToString method

<!-- ... -->

## v1.7.3
Expand Down
10 changes: 6 additions & 4 deletions pkg/crypto/hashing/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hashing

import (
"crypto/sha512"
"sync"

"github.com/number571/go-peer/pkg/encoding"
)
Expand All @@ -15,19 +16,20 @@ const (
)

type sSHA512Hasher struct {
fOnce sync.Once
fHash []byte
fHashStr string
}

func NewHasher(pData []byte) IHasher {
s := sha512.Sum384(pData)
return &sSHA512Hasher{
fHash: s[:],
fHashStr: encoding.HexEncode(s[:]),
}
return &sSHA512Hasher{fHash: s[:]}
}

func (p *sSHA512Hasher) ToString() string {
p.fOnce.Do(func() {
p.fHashStr = encoding.HexEncode(p.ToBytes())
})
return p.fHashStr
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/crypto/hashing/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hashing
import (
"crypto/hmac"
"crypto/sha512"
"sync"

"github.com/number571/go-peer/pkg/encoding"
)
Expand All @@ -12,6 +13,7 @@ var (
)

type sHMACSHA512Hasher struct {
fOnce sync.Once
fHash []byte
fHashStr string
}
Expand All @@ -20,13 +22,13 @@ func NewHMACHasher(pKey []byte, pData []byte) IHasher {
h := hmac.New(sha512.New384, pKey)
h.Write(pData)
s := h.Sum(nil)
return &sHMACSHA512Hasher{
fHash: s,
fHashStr: encoding.HexEncode(s),
}
return &sHMACSHA512Hasher{fHash: s}
}

func (p *sHMACSHA512Hasher) ToString() string {
p.fOnce.Do(func() {
p.fHashStr = encoding.HexEncode(p.ToBytes())
})
return p.fHashStr
}

Expand Down
2 changes: 1 addition & 1 deletion test/result/badge_codelines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bec4fa1

Please sign in to comment.