mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
Package Encoding:
- interface Coder: replace all return io.reader/io.writer to same io interface but with closer implementation - interface Coder: add Reset function to free memory - add sha256: add new sub package that implement coder interface to calculate checksum (no decode available) Other: - bump dependencies
This commit is contained in:
@@ -50,6 +50,12 @@ var _ = Describe("encoding/aes", func() {
|
||||
hnb [12]byte
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if crp != nil {
|
||||
crp.Reset()
|
||||
}
|
||||
}()
|
||||
|
||||
It("Create key must succeed", func() {
|
||||
key, err = encaes.GenKey()
|
||||
Expect(key).ToNot(BeNil())
|
||||
@@ -91,6 +97,7 @@ var _ = Describe("encoding/aes", func() {
|
||||
})
|
||||
|
||||
It("Create new instance must succeed", func() {
|
||||
crp.Reset()
|
||||
crp, err = encaes.New(hkb, hnb)
|
||||
Expect(crp).ToNot(BeNil())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
@@ -107,6 +114,12 @@ var _ = Describe("encoding/aes", func() {
|
||||
non [12]byte
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if crp != nil {
|
||||
crp.Reset()
|
||||
}
|
||||
}()
|
||||
|
||||
It("Create key must succeed", func() {
|
||||
key, err = encaes.GenKey()
|
||||
Expect(key).ToNot(BeNil())
|
||||
@@ -149,10 +162,16 @@ var _ = Describe("encoding/aes", func() {
|
||||
key [32]byte
|
||||
non [12]byte
|
||||
buf = bytes.NewBuffer(make([]byte, 0, 32*1024))
|
||||
rdr io.Reader
|
||||
wrt io.Writer
|
||||
rdr io.ReadCloser
|
||||
wrt io.WriteCloser
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if crp != nil {
|
||||
crp.Reset()
|
||||
}
|
||||
}()
|
||||
|
||||
It("Create key must succeed", func() {
|
||||
key, err = encaes.GenKey()
|
||||
Expect(key).ToNot(BeNil())
|
||||
@@ -178,6 +197,7 @@ var _ = Describe("encoding/aes", func() {
|
||||
nbr, err = wrt.Write(msg)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(nbr).To(BeEquivalentTo(len(msg)))
|
||||
Expect(wrt.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Create and reading an io.reader to decode must succeed", func() {
|
||||
@@ -188,6 +208,7 @@ var _ = Describe("encoding/aes", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(nbr).To(BeEquivalentTo(11))
|
||||
Expect(res[:nbr]).To(BeEquivalentTo(msg[:nbr]))
|
||||
Expect(rdr.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Create an io.reader and read from it to encode string but with small buffer occurs error", func() {
|
||||
@@ -201,6 +222,7 @@ var _ = Describe("encoding/aes", func() {
|
||||
|
||||
nbr, err = rdr.Read(res)
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(rdr.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Create an io.reader and read from it to encode string must succeed", func() {
|
||||
@@ -214,6 +236,7 @@ var _ = Describe("encoding/aes", func() {
|
||||
|
||||
nbr, err = rdr.Read(res)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(rdr.Close()).ToNot(HaveOccurred())
|
||||
res = res[:nbr]
|
||||
})
|
||||
|
||||
@@ -228,6 +251,7 @@ var _ = Describe("encoding/aes", func() {
|
||||
Expect(nbr).To(BeNumerically(">", len(msg)))
|
||||
Expect(buf.Len()).To(BeEquivalentTo(len(msg)))
|
||||
Expect(buf.Bytes()).To(BeEquivalentTo(msg[:buf.Len()]))
|
||||
Expect(wrt.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -43,20 +43,20 @@ type crt struct {
|
||||
}
|
||||
|
||||
func (o *crt) Encode(p []byte) []byte {
|
||||
if len(p) < 1 {
|
||||
if len(p) < 1 || o.a == nil {
|
||||
return make([]byte, 0)
|
||||
}
|
||||
return o.a.Seal(nil, o.n, p, nil)
|
||||
}
|
||||
|
||||
func (o *crt) Decode(p []byte) ([]byte, error) {
|
||||
if len(p) < 1 {
|
||||
if len(p) < 1 || o.a == nil {
|
||||
return make([]byte, 0), nil
|
||||
}
|
||||
return o.a.Open(nil, o.n, p, nil)
|
||||
}
|
||||
|
||||
func (o *crt) EncodeReader(r io.Reader) io.Reader {
|
||||
func (o *crt) EncodeReader(r io.Reader) io.ReadCloser {
|
||||
fct := func(p []byte) (n int, err error) {
|
||||
var b []byte
|
||||
|
||||
@@ -85,12 +85,21 @@ func (o *crt) EncodeReader(r io.Reader) io.Reader {
|
||||
}
|
||||
}
|
||||
|
||||
clo := func() error {
|
||||
if rc, ok := r.(io.Closer); ok {
|
||||
return rc.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return &reader{
|
||||
f: fct,
|
||||
c: clo,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *crt) DecodeReader(r io.Reader) io.Reader {
|
||||
func (o *crt) DecodeReader(r io.Reader) io.ReadCloser {
|
||||
fct := func(p []byte) (n int, err error) {
|
||||
var b = make([]byte, cap(p)+o.a.Overhead())
|
||||
|
||||
@@ -113,12 +122,21 @@ func (o *crt) DecodeReader(r io.Reader) io.Reader {
|
||||
}
|
||||
}
|
||||
|
||||
clo := func() error {
|
||||
if rc, ok := r.(io.Closer); ok {
|
||||
return rc.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return &reader{
|
||||
f: fct,
|
||||
c: clo,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *crt) EncodeWriter(w io.Writer) io.Writer {
|
||||
func (o *crt) EncodeWriter(w io.Writer) io.WriteCloser {
|
||||
fct := func(p []byte) (n int, err error) {
|
||||
n = len(p)
|
||||
if _, err = w.Write(o.Encode(p)); err != nil {
|
||||
@@ -128,12 +146,21 @@ func (o *crt) EncodeWriter(w io.Writer) io.Writer {
|
||||
}
|
||||
}
|
||||
|
||||
clo := func() error {
|
||||
if wc, ok := w.(io.Closer); ok {
|
||||
return wc.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return &writer{
|
||||
f: fct,
|
||||
c: clo,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *crt) DecodeWriter(w io.Writer) io.Writer {
|
||||
func (o *crt) DecodeWriter(w io.Writer) io.WriteCloser {
|
||||
fct := func(p []byte) (n int, err error) {
|
||||
n = len(p)
|
||||
if p, err = o.Decode(p); err != nil {
|
||||
@@ -145,13 +172,30 @@ func (o *crt) DecodeWriter(w io.Writer) io.Writer {
|
||||
}
|
||||
}
|
||||
|
||||
clo := func() error {
|
||||
if wc, ok := w.(io.Closer); ok {
|
||||
return wc.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return &writer{
|
||||
f: fct,
|
||||
c: clo,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *crt) Reset() {
|
||||
o.a = nil
|
||||
|
||||
clear(o.n)
|
||||
o.n = o.n[:0]
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
f func(p []byte) (n int, err error)
|
||||
c func() error
|
||||
}
|
||||
|
||||
func (r *reader) Read(p []byte) (n int, err error) {
|
||||
@@ -162,14 +206,31 @@ func (r *reader) Read(p []byte) (n int, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *reader) Close() error {
|
||||
if r.c == nil {
|
||||
return fmt.Errorf("invalid closer")
|
||||
} else {
|
||||
return r.c()
|
||||
}
|
||||
}
|
||||
|
||||
type writer struct {
|
||||
f func(p []byte) (n int, err error)
|
||||
c func() error
|
||||
}
|
||||
|
||||
func (r *writer) Write(p []byte) (n int, err error) {
|
||||
if r.f == nil {
|
||||
return 0, fmt.Errorf("invalid reader")
|
||||
return 0, fmt.Errorf("invalid writer")
|
||||
} else {
|
||||
return r.f(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *writer) Close() error {
|
||||
if r.c == nil {
|
||||
return fmt.Errorf("invalid closer")
|
||||
} else {
|
||||
return r.c()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ var _ = Describe("encoding/hexa", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(res).ToNot(BeNil())
|
||||
Expect(res).To(BeEquivalentTo(msg)) // bytes.Equal(msg, []byte("Hello World"))(BeNil())
|
||||
crp.Reset()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -73,8 +74,8 @@ var _ = Describe("encoding/hexa", func() {
|
||||
res = make([]byte, len(msg)*2)
|
||||
crp libenc.Coder
|
||||
buf = bytes.NewBuffer(make([]byte, 0, 32*1024))
|
||||
rdr io.Reader
|
||||
wrt io.Writer
|
||||
rdr io.ReadCloser
|
||||
wrt io.WriteCloser
|
||||
)
|
||||
|
||||
It("Create new instance must succeed", func() {
|
||||
@@ -89,6 +90,7 @@ var _ = Describe("encoding/hexa", func() {
|
||||
nbr, err = wrt.Write(msg)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(nbr).To(BeEquivalentTo(len(msg)))
|
||||
Expect(wrt.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Create and reading an io.reader to decode must succeed", func() {
|
||||
@@ -99,6 +101,7 @@ var _ = Describe("encoding/hexa", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(nbr).To(BeEquivalentTo(11))
|
||||
Expect(res[:nbr]).To(BeEquivalentTo(msg[:nbr]))
|
||||
Expect(rdr.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Create an io.reader and read from it to encode string but use too small buffer must occur an error", func() {
|
||||
@@ -112,6 +115,7 @@ var _ = Describe("encoding/hexa", func() {
|
||||
|
||||
nbr, err = rdr.Read(res)
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(rdr.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Create an io.reader and read from it to encode string must succeed", func() {
|
||||
@@ -125,6 +129,7 @@ var _ = Describe("encoding/hexa", func() {
|
||||
|
||||
nbr, err = rdr.Read(res)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(rdr.Close()).ToNot(HaveOccurred())
|
||||
res = res[:nbr]
|
||||
})
|
||||
|
||||
@@ -139,6 +144,7 @@ var _ = Describe("encoding/hexa", func() {
|
||||
Expect(nbr).To(BeNumerically(">", len(msg)))
|
||||
Expect(buf.Len()).To(BeEquivalentTo(len(msg)))
|
||||
Expect(buf.Bytes()).To(BeEquivalentTo(msg[:buf.Len()]))
|
||||
Expect(wrt.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -63,7 +63,7 @@ func (o *crt) Decode(p []byte) ([]byte, error) {
|
||||
return d, e
|
||||
}
|
||||
|
||||
func (o *crt) EncodeReader(r io.Reader) io.Reader {
|
||||
func (o *crt) EncodeReader(r io.Reader) io.ReadCloser {
|
||||
f := func(p []byte) (n int, err error) {
|
||||
var b []byte
|
||||
|
||||
@@ -94,18 +94,46 @@ func (o *crt) EncodeReader(r io.Reader) io.Reader {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return &reader{f: f}
|
||||
c := func() error {
|
||||
if rc, ok := r.(io.Closer); ok {
|
||||
return rc.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return &reader{f: f, c: c}
|
||||
}
|
||||
|
||||
func (o *crt) DecodeReader(r io.Reader) io.Reader {
|
||||
return hex.NewDecoder(r)
|
||||
func (o *crt) DecodeReader(r io.Reader) io.ReadCloser {
|
||||
var h = hex.NewDecoder(r)
|
||||
f := func(p []byte) (n int, err error) {
|
||||
return h.Read(p)
|
||||
}
|
||||
c := func() error {
|
||||
if rc, ok := r.(io.Closer); ok {
|
||||
return rc.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return &reader{f: f, c: c}
|
||||
}
|
||||
|
||||
func (o *crt) EncodeWriter(w io.Writer) io.Writer {
|
||||
return hex.NewEncoder(w)
|
||||
func (o *crt) EncodeWriter(w io.Writer) io.WriteCloser {
|
||||
var h = hex.NewEncoder(w)
|
||||
f := func(p []byte) (n int, err error) {
|
||||
return h.Write(p)
|
||||
}
|
||||
c := func() error {
|
||||
if wc, ok := w.(io.Closer); ok {
|
||||
return wc.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return &writer{f: f, c: c}
|
||||
}
|
||||
|
||||
func (o *crt) DecodeWriter(w io.Writer) io.Writer {
|
||||
func (o *crt) DecodeWriter(w io.Writer) io.WriteCloser {
|
||||
f := func(p []byte) (n int, err error) {
|
||||
var b []byte
|
||||
n = len(p)
|
||||
@@ -125,11 +153,21 @@ func (o *crt) DecodeWriter(w io.Writer) io.Writer {
|
||||
}
|
||||
}
|
||||
}
|
||||
return &writer{f: f}
|
||||
c := func() error {
|
||||
if wc, ok := w.(io.Closer); ok {
|
||||
return wc.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return &writer{f: f, c: c}
|
||||
}
|
||||
|
||||
func (o *crt) Reset() {
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
f func(p []byte) (n int, err error)
|
||||
c func() error
|
||||
}
|
||||
|
||||
func (r *reader) Read(p []byte) (n int, err error) {
|
||||
@@ -140,8 +178,17 @@ func (r *reader) Read(p []byte) (n int, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *reader) Close() error {
|
||||
if r.c == nil {
|
||||
return fmt.Errorf("invalid closer")
|
||||
} else {
|
||||
return r.c()
|
||||
}
|
||||
}
|
||||
|
||||
type writer struct {
|
||||
f func(p []byte) (n int, err error)
|
||||
c func() error
|
||||
}
|
||||
|
||||
func (r *writer) Write(p []byte) (n int, err error) {
|
||||
@@ -151,3 +198,11 @@ func (r *writer) Write(p []byte) (n int, err error) {
|
||||
return r.f(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *writer) Close() error {
|
||||
if r.c == nil {
|
||||
return fmt.Errorf("invalid closer")
|
||||
} else {
|
||||
return r.c()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,23 +51,26 @@ type Coder interface {
|
||||
//
|
||||
// r io.Reader
|
||||
// io.Reader
|
||||
EncodeReader(r io.Reader) io.Reader
|
||||
EncodeReader(r io.Reader) io.ReadCloser
|
||||
|
||||
// DecodeReader return a io.Reader that can be used to decode the given byte slice
|
||||
//
|
||||
// r io.Reader
|
||||
// io.Reader
|
||||
DecodeReader(r io.Reader) io.Reader
|
||||
DecodeReader(r io.Reader) io.ReadCloser
|
||||
|
||||
// EncodeWriter return a io.writer that can be used to encode the given byte slice
|
||||
//
|
||||
// w io.Writer parameter.
|
||||
// io.Writer return type.
|
||||
EncodeWriter(w io.Writer) io.Writer
|
||||
EncodeWriter(w io.Writer) io.WriteCloser
|
||||
|
||||
// DecodeWriter return a io.writer that can be used to decode the given byte slice
|
||||
//
|
||||
// w io.Writer parameter.
|
||||
// io.Writer return type.
|
||||
DecodeWriter(w io.Writer) io.Writer
|
||||
DecodeWriter(w io.Writer) io.WriteCloser
|
||||
|
||||
// Reset will free memory
|
||||
Reset()
|
||||
}
|
||||
|
||||
39
encoding/sha256/interface.go
Normal file
39
encoding/sha256/interface.go
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2023 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package sha256
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
|
||||
libenc "github.com/nabbar/golib/encoding"
|
||||
)
|
||||
|
||||
func New() libenc.Coder {
|
||||
return &crt{
|
||||
hsh: sha256.New(),
|
||||
}
|
||||
}
|
||||
159
encoding/sha256/model.go
Normal file
159
encoding/sha256/model.go
Normal file
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2023 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package sha256
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
)
|
||||
|
||||
type crt struct {
|
||||
hsh hash.Hash
|
||||
}
|
||||
|
||||
func (o *crt) Encode(p []byte) []byte {
|
||||
if o.hsh == nil {
|
||||
return make([]byte, 0)
|
||||
}
|
||||
|
||||
if len(p) > 0 {
|
||||
if _, e := o.hsh.Write(p); e != nil {
|
||||
return make([]byte, 0)
|
||||
}
|
||||
}
|
||||
|
||||
if q := o.hsh.Sum(nil); len(q) < 1 {
|
||||
return make([]byte, 0)
|
||||
} else {
|
||||
return q[:]
|
||||
}
|
||||
}
|
||||
|
||||
func (o *crt) Decode(p []byte) ([]byte, error) {
|
||||
return nil, fmt.Errorf("unexpected call function")
|
||||
}
|
||||
|
||||
func (o *crt) EncodeReader(r io.Reader) io.ReadCloser {
|
||||
f := func(p []byte) (n int, err error) {
|
||||
n, err = r.Read(p)
|
||||
|
||||
if n > 0 && o.hsh != nil {
|
||||
o.hsh.Write(p[0:n])
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
c := func() error {
|
||||
if rc, ok := r.(io.Closer); ok {
|
||||
return rc.Close()
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return &reader{f: f, c: c}
|
||||
}
|
||||
|
||||
func (o *crt) DecodeReader(r io.Reader) io.ReadCloser {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *crt) EncodeWriter(w io.Writer) io.WriteCloser {
|
||||
f := func(p []byte) (n int, err error) {
|
||||
n, err = w.Write(p)
|
||||
|
||||
if n > 0 && o.hsh != nil {
|
||||
o.hsh.Write(p[0:n])
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
c := func() error {
|
||||
if wc, ok := w.(io.Closer); ok {
|
||||
return wc.Close()
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return &writer{f: f, c: c}
|
||||
}
|
||||
|
||||
func (o *crt) DecodeWriter(w io.Writer) io.WriteCloser {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *crt) Reset() {
|
||||
if o.hsh != nil {
|
||||
o.hsh.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
f func(p []byte) (n int, err error)
|
||||
c func() error
|
||||
}
|
||||
|
||||
func (r *reader) Read(p []byte) (n int, err error) {
|
||||
if r.f == nil {
|
||||
return 0, fmt.Errorf("invalid reader")
|
||||
} else {
|
||||
return r.f(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *reader) Close() error {
|
||||
if r.c == nil {
|
||||
return fmt.Errorf("invalid closer")
|
||||
} else {
|
||||
return r.c()
|
||||
}
|
||||
}
|
||||
|
||||
type writer struct {
|
||||
f func(p []byte) (n int, err error)
|
||||
c func() error
|
||||
}
|
||||
|
||||
func (r *writer) Write(p []byte) (n int, err error) {
|
||||
if r.f == nil {
|
||||
return 0, fmt.Errorf("invalid writer")
|
||||
} else {
|
||||
return r.f(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *writer) Close() error {
|
||||
if r.c == nil {
|
||||
return fmt.Errorf("invalid closer")
|
||||
} else {
|
||||
return r.c()
|
||||
}
|
||||
}
|
||||
53
encoding/sha256/sha256_suite_test.go
Normal file
53
encoding/sha256/sha256_suite_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2023 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package sha256_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
/*
|
||||
Using https://onsi.github.io/ginkgo/
|
||||
Running with $> ginkgo -cover .
|
||||
*/
|
||||
|
||||
// TestGolibEncodingHexHelper tests the Golib Hex Encoding Helper function.
|
||||
func TestGolibEncodingSHA256Helper(t *testing.T) {
|
||||
time.Sleep(500 * time.Millisecond) // Adding delay for better testing synchronization
|
||||
RegisterFailHandler(Fail) // Registering fail handler for better test failure reporting
|
||||
RunSpecs(t, "Encoding SHA256 Helper Suite") // Running the test suite for Encoding Hex Helper
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
})
|
||||
|
||||
var _ = AfterSuite(func() {
|
||||
})
|
||||
135
encoding/sha256/sha256_test.go
Normal file
135
encoding/sha256/sha256_test.go
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2023 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package sha256_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"io"
|
||||
|
||||
libenc "github.com/nabbar/golib/encoding"
|
||||
encsha "github.com/nabbar/golib/encoding/sha256"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("encoding/sha256", func() {
|
||||
Context("Simple encoding/decoding", func() {
|
||||
var (
|
||||
err error
|
||||
msg = []byte("Hello World")
|
||||
sig []byte
|
||||
crp libenc.Coder
|
||||
chk = sha256.Sum256(msg)
|
||||
)
|
||||
|
||||
It("Create new instance must succeed", func() {
|
||||
crp = encsha.New()
|
||||
Expect(crp).ToNot(BeNil())
|
||||
})
|
||||
|
||||
It("must succeed to encode", func() {
|
||||
sig = crp.Encode(msg)
|
||||
Expect(sig).ToNot(BeNil())
|
||||
Expect(sig).To(BeEquivalentTo(chk[:])) // bytes.Equal(msg, []byte("Hello World"))(BeNil())
|
||||
crp.Reset()
|
||||
})
|
||||
|
||||
It("must return error when decode is call", func() {
|
||||
crp = encsha.New()
|
||||
Expect(crp).ToNot(BeNil())
|
||||
|
||||
_, err = crp.Decode(msg)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
Context("IO interface with encoding/decoding", func() {
|
||||
var (
|
||||
err error
|
||||
nbr int
|
||||
msg = []byte("Hello World")
|
||||
res = make([]byte, len(msg)*2)
|
||||
crp libenc.Coder
|
||||
buf = bytes.NewBuffer(make([]byte, 0, 32*1024))
|
||||
rdr io.ReadCloser
|
||||
wrt io.WriteCloser
|
||||
chk = (sha256.Sum256(msg))
|
||||
)
|
||||
|
||||
It("Create new instance must succeed", func() {
|
||||
crp = encsha.New()
|
||||
Expect(crp).ToNot(BeNil())
|
||||
})
|
||||
|
||||
It("Create and write an io.writer to encode must succeed", func() {
|
||||
wrt = crp.EncodeWriter(buf)
|
||||
Expect(wrt).ToNot(BeNil())
|
||||
|
||||
nbr, err = wrt.Write(msg)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(nbr).To(BeEquivalentTo(len(msg)))
|
||||
Expect(buf.Bytes()).To(BeEquivalentTo(msg))
|
||||
Expect(crp.Encode(nil)).To(BeEquivalentTo(chk))
|
||||
Expect(wrt.Close()).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Reset and Create new instance must succeed", func() {
|
||||
crp.Reset()
|
||||
crp = encsha.New()
|
||||
Expect(crp).ToNot(BeNil())
|
||||
})
|
||||
|
||||
It("Create an io.reader and read from it to encode string must succeed", func() {
|
||||
buf.Reset()
|
||||
buf.Write(msg)
|
||||
|
||||
rdr = crp.EncodeReader(buf)
|
||||
Expect(rdr).ToNot(BeNil())
|
||||
|
||||
res, err = io.ReadAll(rdr)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(len(res)).To(BeEquivalentTo(len(msg)))
|
||||
Expect(res).To(BeEquivalentTo(msg))
|
||||
Expect(crp.Encode(nil)).To(BeEquivalentTo(chk))
|
||||
Expect(rdr.Close()).ToNot(HaveOccurred())
|
||||
crp.Reset()
|
||||
})
|
||||
|
||||
It("must return error when reader/writer decoder is call", func() {
|
||||
crp = encsha.New()
|
||||
Expect(crp).ToNot(BeNil())
|
||||
|
||||
rdr = crp.DecodeReader(bytes.NewReader(msg))
|
||||
Expect(rdr).To(BeNil())
|
||||
|
||||
buf.Reset()
|
||||
wrt = crp.DecodeWriter(buf)
|
||||
Expect(rdr).To(BeNil())
|
||||
})
|
||||
})
|
||||
})
|
||||
6
go.mod
6
go.mod
@@ -12,7 +12,7 @@ require (
|
||||
github.com/aws/aws-sdk-go-v2/service/iam v1.40.1
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.78.2
|
||||
github.com/aws/smithy-go v1.22.3
|
||||
github.com/bits-and-blooms/bitset v1.21.0
|
||||
github.com/bits-and-blooms/bitset v1.22.0
|
||||
github.com/c-bata/go-prompt v0.2.6
|
||||
github.com/dsnet/compress v0.0.1
|
||||
github.com/fatih/color v1.18.0
|
||||
@@ -91,7 +91,7 @@ require (
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.29.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.33.17 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bytedance/sonic v1.13.0 // indirect
|
||||
github.com/bytedance/sonic v1.13.1 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
@@ -152,7 +152,7 @@ require (
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pkg/term v1.2.0-beta.2 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.62.0 // indirect
|
||||
github.com/prometheus/common v0.63.0 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
|
||||
Reference in New Issue
Block a user