Denmaseno %!s(int64=8) %!d(string=hai) anos
pai
achega
c420be7c70
Modificáronse 3 ficheiros con 76 adicións e 6 borrados
  1. 24 0
      encrypt-aes_test.go
  2. 52 0
      signer.go
  3. 0 6
      util.go

+ 24 - 0
encrypt-aes_test.go

@@ -0,0 +1,24 @@
+package util
+
+import (
+	"bytes"
+	"testing"
+)
+
+// TestAES test
+func TestAES(t *testing.T) {
+	var bb bytes.Buffer
+	for bb.Len() < 1024 {
+		bb.Write([]byte("this is a very long text. "))
+	}
+	b := bb.Bytes()
+	t.Logf("DATA  %v %s", len(b), string(b))
+	key := KeyAES()
+	x := EncryptAES(key, b)
+	t.Logf("ENC LEN %v", len(x))
+	p := DecryptAES(key, x)
+	t.Logf("PLAIN %v %s", len(p), string(p))
+	if !bytes.Equal(b, p) {
+		t.Errorf("ERROR\n%s\n%s", string(b), string(p))
+	}
+}

+ 52 - 0
signer.go

@@ -0,0 +1,52 @@
+package util
+
+import (
+	"crypto"
+	"crypto/rand"
+	"crypto/rsa"
+	"fmt"
+	"hash"
+
+	"code.senomas.com/go/util"
+)
+
+// Signer struct
+type Signer struct {
+	opts   rsa.PSSOptions
+	method crypto.Hash
+	h      hash.Hash
+}
+
+// NewSigner func
+func NewSigner() *Signer {
+	hash := crypto.SHA256
+	var opts rsa.PSSOptions
+	opts.SaltLength = rsa.PSSSaltLengthAuto
+	return &Signer{opts, hash, hash.New()}
+}
+
+func (signer *Signer) Write(p []byte) (n int, err error) {
+	signer.h.Write(p)
+	return
+}
+
+// WriteUint64 func
+func (signer *Signer) WriteUint64(v uint64) {
+	signer.h.Write(util.Uint64ToBytes(v))
+}
+
+// Sign func
+func (signer *Signer) Sign(key *rsa.PrivateKey) []byte {
+	if res, err := rsa.SignPSS(rand.Reader, key, signer.method, signer.h.Sum(nil), &signer.opts); err != nil {
+		panic(err)
+	} else {
+		return res
+	}
+}
+
+// Verify func
+func (signer *Signer) Verify(key *rsa.PublicKey, sum []byte) {
+	if err := rsa.VerifyPSS(key, signer.method, signer.h.Sum(nil), sum, &signer.opts); err != nil {
+		panic(fmt.Errorf("Verify failed"))
+	}
+}

+ 0 - 6
util.go

@@ -1,16 +1,10 @@
 package util
 
 import (
-	"crypto/rsa"
 	"encoding/binary"
 	"fmt"
 )
 
-// Signer interface
-type Signer interface {
-	GetPrivateKey() *rsa.PrivateKey
-}
-
 // Check error function
 func Check(format string, args ...interface{}) {
 	var hasError bool