]> 127.0.0.1 Git - part/.git/commitdiff
KeyStatus
authorqydysky <qydysky@foxmail.com>
Thu, 4 Mar 2021 08:00:51 +0000 (16:00 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 4 Mar 2021 08:00:51 +0000 (16:00 +0800)
crypto/Crypto.go
crypto/Crypto_test.go

index 36f715abc2a432d80d13e9b2a92014a97665214d..ce3de827eeff3041a0480205d1fe13e6903f56ac 100644 (file)
@@ -11,6 +11,11 @@ import (
        "encoding/pem"
 )
 
+var (
+       PublicKeyNoLoad = errors.New(`PublicKeyNoLoad`)
+       PrivateKeyNoLoad = errors.New(`PrivateKeyNoLoad`)
+)
+
 type Crypto struct {
        pubKey *rsa.PublicKey
        priKey *rsa.PrivateKey
@@ -31,6 +36,16 @@ func FileLoad(path string) (data []byte, err error) {
        return
 }
 
+func (t *Crypto) KeyStatus() (error) {
+       if t.pubKey == nil {
+               return PublicKeyNoLoad
+       }
+       if t.priKey == nil {
+               return PrivateKeyNoLoad
+       }
+       return nil
+}
+
 func (t *Crypto) GetPKIXPubKey(pubPEMData []byte) (err error) {
        block, _ := pem.Decode(pubPEMData)
        if block == nil || block.Type != "PUBLIC KEY" {
index 8ebfd7851ffb754c2f0a728dfe0abb6bfdb97452..baf40866f9bd48e849aeae0e28eb219804665c52 100644 (file)
@@ -4,14 +4,17 @@ import "testing"
 
 func Test(t *testing.T){
        var k Crypto
+       if k.KeyStatus() != PublicKeyNoLoad {t.Error(`Keystatus not PublicKeyNoLoad`)}
        {
                d,_ := FileLoad(`public.pem`)
                k.GetPKIXPubKey(d)
        }
+       if k.KeyStatus() != PrivateKeyNoLoad {t.Error(`Keystatus not PrivateKeyNoLoad`)}
        {
                d,_ := FileLoad(`private.pem`)
                k.GetPKCS1PriKey(d)
        }
+       if k.KeyStatus() != nil {t.Error(`Keystatus not nil`)}
        if srcs,e := k.GetEncrypt([]byte(`1we23`));e != nil {
                t.Error(e)
        } else if des,e := k.GetDecrypt(srcs);e != nil {