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

index ce3de827eeff3041a0480205d1fe13e6903f56ac..acbee35a77e828ac81dd918d7ffc7079007dd423 100644 (file)
@@ -1,4 +1,4 @@
-package main
+package part
 
 import (
        "os"
@@ -11,11 +11,6 @@ import (
        "encoding/pem"
 )
 
-var (
-       PublicKeyNoLoad = errors.New(`PublicKeyNoLoad`)
-       PrivateKeyNoLoad = errors.New(`PrivateKeyNoLoad`)
-)
-
 type Crypto struct {
        pubKey *rsa.PublicKey
        priKey *rsa.PrivateKey
@@ -36,14 +31,12 @@ 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) PubLoad() (bool) {
+       return t.pubKey != nil
+}
+
+func (t *Crypto) PriLoad() (bool) {
+       return t.priKey != nil
 }
 
 func (t *Crypto) GetPKIXPubKey(pubPEMData []byte) (err error) {
index baf40866f9bd48e849aeae0e28eb219804665c52..2f87acc81d1a0ae6c0ed88f3821a0d73bf3b3484 100644 (file)
@@ -1,20 +1,20 @@
-package main
+package part
  
 import "testing"
 
 func Test(t *testing.T){
        var k Crypto
-       if k.KeyStatus() != PublicKeyNoLoad {t.Error(`Keystatus not PublicKeyNoLoad`)}
+       if k.PubLoad() || k.PriLoad() {t.Error(`Keystatus not PublicKeyNoLoad`)}
        {
                d,_ := FileLoad(`public.pem`)
                k.GetPKIXPubKey(d)
        }
-       if k.KeyStatus() != PrivateKeyNoLoad {t.Error(`Keystatus not PrivateKeyNoLoad`)}
+       if !k.PubLoad() || k.PriLoad() {t.Error(`Keystatus not PrivateKeyNoLoad`)}
        {
                d,_ := FileLoad(`private.pem`)
                k.GetPKCS1PriKey(d)
        }
-       if k.KeyStatus() != nil {t.Error(`Keystatus not nil`)}
+       if !k.PubLoad() || !k.PriLoad() {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 {