Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
3JoB committed Sep 26, 2023
1 parent 581315f commit 6847ffb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
4 changes: 2 additions & 2 deletions hash/hmac/hamc_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func MD5S(data, key string) *hash.Hash {
// Its generic security strength is 128 bits against all attacks
// if at least 32 bytes of its output are used.
func Shake128S(data string, bits int) string {
return Shake128(unsafeConvert.BytePointer(data), bits)
return unsafeConvert.StringSlice(Shake128(unsafeConvert.ByteSlice(data), bits))
}

// NewShake256 creates a new SHAKE256 variable-output-length ShakeHash.
// Its generic security strength is 256 bits against all attacks
// if at least 64 bytes of its output are used.
func Shake256S(data string, bits int) string {
return Shake256(unsafeConvert.BytePointer(data), bits)
return unsafeConvert.StringSlice(Shake256(unsafeConvert.ByteSlice(data), bits))
}
34 changes: 9 additions & 25 deletions hash/hmac/hmac.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package hmac

import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
hs "hash"
"sync"

"github.com/3JoB/unsafeConvert"
"golang.org/x/crypto/sha3"
Expand All @@ -17,17 +15,7 @@ import (
)

var (
pubkey = unsafeConvert.BytePointer("ulib-hmac")
shake128pool = &sync.Pool{
New: func() any {
return sha3.NewShake128()
},
}
shake256pool = &sync.Pool{
New: func() any {
return sha3.NewShake256()
},
}
pubkey = unsafeConvert.BytePointer("ulib-hmac")
)

func c(h func() hs.Hash, data, key []byte) *hash.Hash {
Expand Down Expand Up @@ -84,8 +72,8 @@ func MD5(data, key []byte) *hash.Hash {
// NewShake128 creates a new SHAKE128 variable-output-length ShakeHash.
// Its generic security strength is 128 bits against all attacks
// if at least 32 bytes of its output are used.
func Shake128(data []byte, bits int) string {
shake := shake128pool.Get().(sha3.ShakeHash)
func Shake128(data []byte, bits int) []byte {
shake := hash.AcquireShake128()
shake.Write(data)
if bits > 128 {
bits = 128
Expand All @@ -94,27 +82,23 @@ func Shake128(data []byte, bits int) string {
}
h := make([]byte, bits)
shake.Read(h)
shake.Reset()
shake128pool.Put(shake)
return hex.EncodeToString(h)
hash.ReleaseShake128(shake)
return hex.Encode(h)
}

// NewShake256 creates a new SHAKE256 variable-output-length ShakeHash.
// Its generic security strength is 256 bits against all attacks
// if at least 64 bytes of its output are used.
func Shake256(data []byte, bits int) string {
shake := shake256pool.Get().(sha3.ShakeHash)
func Shake256(data []byte, bits int) []byte {
shake := hash.AcquireShake256()
shake.Write(data)
if bits > 256 {
bits = 256
} else if bits < 64 {
bits = 64
}
var g bytes.Buffer
h := make([]byte, bits)
g.ReadFrom(shake)
shake.Read(h)
shake.Reset()
shake256pool.Put(shake)
return hex.EncodeToString(h)
hash.ReleaseShake256(shake)
return hex.Encode(h)
}
14 changes: 14 additions & 0 deletions hex/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ import (
"github.com/3JoB/unsafeConvert"
)

// Encode encodes src into EncodedLen(len(src)) bytes of dst. As a convenience,
// it returns the number of bytes written to dst, but this value is always
// EncodedLen(len(src)). Encode implements hexadecimal encoding.
func Encode(src []byte) []byte {
dst := make([]byte, len(src)*2)
hex.Encode(dst, src)
return dst
}

// EncodeToString returns the hexadecimal encoding of src.
func EncodeToString(src []byte) string {
dst := make([]byte, len(src)*2)
hex.Encode(dst, src)
return unsafeConvert.StringPointer(dst)
}

func Decode(b []byte) ([]byte, error) {
n, err := hex.Decode(b, b)
return b[:n], err
}

// DecodeString returns the bytes represented by the hexadecimal string s.
//
// DecodeString expects that src contains only hexadecimal
Expand Down
38 changes: 38 additions & 0 deletions internal/hash/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hash

import (
"sync"

"golang.org/x/crypto/sha3"
)

var (
shake128pool = &sync.Pool{
New: func() any {
return sha3.NewShake128()
},
}
shake256pool = &sync.Pool{
New: func() any {
return sha3.NewShake256()
},
}
)

func AcquireShake128() sha3.ShakeHash {
return shake128pool.Get().(sha3.ShakeHash)
}

func AcquireShake256() sha3.ShakeHash {
return shake256pool.Get().(sha3.ShakeHash)
}

func ReleaseShake128(s sha3.ShakeHash) {
s.Reset()
shake128pool.Put(s)
}

func ReleaseShake256(s sha3.ShakeHash) {
s.Reset()
shake256pool.Put(s)
}

0 comments on commit 6847ffb

Please sign in to comment.