mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
bytesconv
This commit is contained in:
24
bytesconv/byteResult.go
Normal file
24
bytesconv/byteResult.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package bytesconv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BytesResult []byte
|
||||||
|
|
||||||
|
func (r BytesResult) Hex() string {
|
||||||
|
return hex.EncodeToString(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r BytesResult) Base64() string {
|
||||||
|
return base64.StdEncoding.EncodeToString(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r BytesResult) Bytes() []byte {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r BytesResult) String() string {
|
||||||
|
return r.Hex()
|
||||||
|
}
|
@ -1,8 +1,20 @@
|
|||||||
package bytesconv
|
package bytesconv
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestBytesToUint64(t *testing.T) {
|
func TestBytesToUint64(t *testing.T) {
|
||||||
t.Log(BigEndian.BytesToUInt64([]byte{0x88, 0x45}))
|
t.Log(BigEndian.BytesToUInt64([]byte{0x88, 0x45}))
|
||||||
t.Log(LittleEndian.BytesToUInt64([]byte{0x88, 0x45}))
|
t.Log(LittleEndian.BytesToUInt64([]byte{0x88, 0x45}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkBytesToUInt64(b *testing.B) {
|
||||||
|
aa := []byte("a")
|
||||||
|
|
||||||
|
b.Run("1", func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
BigEndian.BytesToUInt64(aa)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package bytesconv
|
package bytesconv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,36 +18,3 @@ func StringToBytes(s string) (b []byte) {
|
|||||||
func BytesToString(b []byte) string {
|
func BytesToString(b []byte) string {
|
||||||
return *(*string)(unsafe.Pointer(&b))
|
return *(*string)(unsafe.Pointer(&b))
|
||||||
}
|
}
|
||||||
|
|
||||||
type endian int
|
|
||||||
|
|
||||||
const (
|
|
||||||
BigEndian endian = iota + 1
|
|
||||||
LittleEndian
|
|
||||||
)
|
|
||||||
|
|
||||||
func (e endian) BytesToUInt64(data []byte) (uint64, error) {
|
|
||||||
if len(data) > 8 {
|
|
||||||
return 0, fmt.Errorf("bytes to uint64, bytes length is invaild")
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret uint64
|
|
||||||
var len int = len(data)
|
|
||||||
|
|
||||||
if e == BigEndian {
|
|
||||||
for i := 0; i < len; i++ {
|
|
||||||
ret = ret | (uint64(data[len-1-i]) << (i * 8))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for i := 0; i < len; i++ {
|
|
||||||
ret = ret | (uint64(data[i]) << (i * 8))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func BytesToUInt64Big() {
|
|
||||||
// binary.BigEndian
|
|
||||||
// binary.LittleEndian
|
|
||||||
}
|
|
||||||
|
31
bytesconv/uint64.go
Normal file
31
bytesconv/uint64.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package bytesconv
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type endian int
|
||||||
|
|
||||||
|
const (
|
||||||
|
BigEndian endian = iota + 1
|
||||||
|
LittleEndian
|
||||||
|
)
|
||||||
|
|
||||||
|
func (e endian) BytesToUInt64(data []byte) (uint64, error) {
|
||||||
|
if len(data) > 8 {
|
||||||
|
return 0, fmt.Errorf("bytes to uint64, bytes length is invaild")
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret uint64
|
||||||
|
var len int = len(data)
|
||||||
|
|
||||||
|
if e == BigEndian {
|
||||||
|
for i := 0; i < len; i++ {
|
||||||
|
ret = ret | (uint64(data[len-1-i]) << (i * 8))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for i := 0; i < len; i++ {
|
||||||
|
ret = ret | (uint64(data[i]) << (i * 8))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
37
hash/hash.go
37
hash/hash.go
@ -5,29 +5,28 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/hex"
|
|
||||||
"hash"
|
"hash"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
|
|
||||||
"github.com/cespare/xxhash/v2"
|
"github.com/cespare/xxhash/v2"
|
||||||
|
"github.com/charlienet/go-mixed/bytesconv"
|
||||||
"github.com/spaolacci/murmur3"
|
"github.com/spaolacci/murmur3"
|
||||||
"github.com/tjfoc/gmsm/sm3"
|
"github.com/tjfoc/gmsm/sm3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Md5(msg []byte) hashResult { return sum(md5.New, msg) }
|
func Md5(msg []byte) bytesconv.BytesResult { return sum(md5.New, msg) }
|
||||||
|
|
||||||
func Sha1(msg []byte) hashResult { return sum(sha1.New, msg) }
|
func Sha1(msg []byte) bytesconv.BytesResult { return sum(sha1.New, msg) }
|
||||||
|
|
||||||
func Sha224(msg []byte) hashResult { return sum(sha256.New224, msg) }
|
func Sha224(msg []byte) bytesconv.BytesResult { return sum(sha256.New224, msg) }
|
||||||
|
|
||||||
func Sha256(msg []byte) hashResult { return sum(sha256.New, msg) }
|
func Sha256(msg []byte) bytesconv.BytesResult { return sum(sha256.New, msg) }
|
||||||
|
|
||||||
func Sha384(msg []byte) hashResult { return sum(sha512.New384, msg) }
|
func Sha384(msg []byte) bytesconv.BytesResult { return sum(sha512.New384, msg) }
|
||||||
|
|
||||||
func Sha512(msg []byte) hashResult { return sum(sha512.New, msg) }
|
func Sha512(msg []byte) bytesconv.BytesResult { return sum(sha512.New, msg) }
|
||||||
|
|
||||||
func Sm3(msg []byte) hashResult { return sum(sm3.New, msg) }
|
func Sm3(msg []byte) bytesconv.BytesResult { return sum(sm3.New, msg) }
|
||||||
|
|
||||||
func Murmur3(msg []byte) uint64 {
|
func Murmur3(msg []byte) uint64 {
|
||||||
return murmur3.Sum64(msg)
|
return murmur3.Sum64(msg)
|
||||||
@ -57,27 +56,9 @@ func Funv64(msg []byte) uint64 {
|
|||||||
return h.Sum64()
|
return h.Sum64()
|
||||||
}
|
}
|
||||||
|
|
||||||
type hashResult []byte
|
func sum(f func() hash.Hash, msg []byte) bytesconv.BytesResult {
|
||||||
|
|
||||||
func sum(f func() hash.Hash, msg []byte) hashResult {
|
|
||||||
h := f()
|
h := f()
|
||||||
|
|
||||||
_, _ = h.Write(msg)
|
_, _ = h.Write(msg)
|
||||||
return h.Sum(nil)
|
return h.Sum(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r hashResult) Hex() string {
|
|
||||||
return hex.EncodeToString(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r hashResult) Base64() string {
|
|
||||||
return base64.StdEncoding.EncodeToString(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r hashResult) Bytes() []byte {
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r hashResult) String() string {
|
|
||||||
return r.Hex()
|
|
||||||
}
|
|
||||||
|
37
hmac/hmac.go
37
hmac/hmac.go
@ -6,48 +6,29 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/hex"
|
|
||||||
"hash"
|
"hash"
|
||||||
|
|
||||||
|
"github.com/charlienet/go-mixed/bytesconv"
|
||||||
"github.com/tjfoc/gmsm/sm3"
|
"github.com/tjfoc/gmsm/sm3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Md5(key, msg []byte) hashResult { return sum(md5.New, key, msg) }
|
func Md5(key, msg []byte) bytesconv.BytesResult { return sum(md5.New, key, msg) }
|
||||||
|
|
||||||
func Sha1(key, msg []byte) hashResult { return sum(sha1.New, key, msg) }
|
func Sha1(key, msg []byte) bytesconv.BytesResult { return sum(sha1.New, key, msg) }
|
||||||
|
|
||||||
func Sha224(key, msg []byte) hashResult { return sum(sha256.New224, key, msg) }
|
func Sha224(key, msg []byte) bytesconv.BytesResult { return sum(sha256.New224, key, msg) }
|
||||||
|
|
||||||
func Sha256(key, msg []byte) hashResult { return sum(sha256.New, key, msg) }
|
func Sha256(key, msg []byte) bytesconv.BytesResult { return sum(sha256.New, key, msg) }
|
||||||
|
|
||||||
func Sha384(key, msg []byte) hashResult { return sum(sha512.New384, key, msg) }
|
func Sha384(key, msg []byte) bytesconv.BytesResult { return sum(sha512.New384, key, msg) }
|
||||||
|
|
||||||
func Sha512(key, msg []byte) hashResult { return sum(sha512.New, key, msg) }
|
func Sha512(key, msg []byte) bytesconv.BytesResult { return sum(sha512.New, key, msg) }
|
||||||
|
|
||||||
func Sm3(key, msg []byte) hashResult { return sum(sm3.New, key, msg) }
|
func Sm3(key, msg []byte) bytesconv.BytesResult { return sum(sm3.New, key, msg) }
|
||||||
|
|
||||||
func sum(f func() hash.Hash, msg, key []byte) hashResult {
|
func sum(f func() hash.Hash, msg, key []byte) bytesconv.BytesResult {
|
||||||
h := hmac.New(f, key)
|
h := hmac.New(f, key)
|
||||||
|
|
||||||
h.Write(msg)
|
h.Write(msg)
|
||||||
return h.Sum(nil)
|
return h.Sum(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
type hashResult []byte
|
|
||||||
|
|
||||||
func (r hashResult) Hex() string {
|
|
||||||
return hex.EncodeToString(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r hashResult) Base64() string {
|
|
||||||
return base64.StdEncoding.EncodeToString(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r hashResult) Bytes() []byte {
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r hashResult) String() string {
|
|
||||||
return r.Hex()
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user