sequences update and async crypto keys optimization
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
74
asymcrypt.go
74
asymcrypt.go
@@ -23,16 +23,20 @@ func NewKeyPair() (*KeyPair, error) { // Return error!
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("key generation failed: %w", err)
|
||||
}
|
||||
pub, err := keys.GetArmoredPublicKey()
|
||||
pubKey, err := keys.ToPublic()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gopenpgp: unable to get public key: %w", err)
|
||||
return nil, fmt.Errorf("gopenpgp: unable to extract public key: %w", err)
|
||||
}
|
||||
priv, err := keys.Armor()
|
||||
pubBytes, err := pubKey.Serialize()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to armor private key: %w", err)
|
||||
return nil, fmt.Errorf("gopenpgp: unable to serialize public key: %w", err)
|
||||
}
|
||||
kp.Public = base64.StdEncoding.EncodeToString([]byte(pub))
|
||||
kp.Private = base64.StdEncoding.EncodeToString([]byte(priv))
|
||||
privBytes, err := keys.Serialize()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to serialize private key: %w", err)
|
||||
}
|
||||
kp.Public = base64.StdEncoding.EncodeToString(pubBytes)
|
||||
kp.Private = base64.StdEncoding.EncodeToString(privBytes)
|
||||
kp.Generated = time.Now()
|
||||
return &kp, nil
|
||||
}
|
||||
@@ -42,9 +46,9 @@ func (Kp *KeyPair) GetCryptoKeyObject() (*crypto.Key, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode private key: %w", err)
|
||||
}
|
||||
key, err := crypto.NewKeyFromArmored(string(priv))
|
||||
key, err := crypto.NewKey(priv)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Ccreate key from armoured failed: %w", err)
|
||||
return nil, fmt.Errorf("create key from binary failed: %w", err)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
@@ -54,7 +58,7 @@ func AsymEncrypt(publicKey string, data []byte) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption b64 failed: %w", err)
|
||||
}
|
||||
ciphertext, err := encryptMessage(string(pub), crypto.NewPlainMessage(data))
|
||||
ciphertext, err := encryptMessage(pub, crypto.NewPlainMessage(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption failed: %w", err)
|
||||
}
|
||||
@@ -67,7 +71,7 @@ func AsymDecrypt(PrivateKey string, data []byte) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption b64 failed: %w", err)
|
||||
}
|
||||
decrypted, err := decryptMessage(string(priv), nil, crypto.NewPGPMessage(data))
|
||||
decrypted, err := decryptMessage(priv, nil, crypto.NewPGPMessage(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption failed: %w", err)
|
||||
}
|
||||
@@ -79,7 +83,15 @@ func AsymEncryptArmored(PublicKey string, data []byte) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption b64 failed: %w", err)
|
||||
}
|
||||
armor, err := helper.EncryptBinaryMessageArmored(string(pub), data)
|
||||
pubKey, err := crypto.NewKey(pub)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption key parse failed: %w", err)
|
||||
}
|
||||
armoredPub, err := pubKey.GetArmoredPublicKey()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption key armor failed: %w", err)
|
||||
}
|
||||
armor, err := helper.EncryptBinaryMessageArmored(armoredPub, data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption failed: %w", err)
|
||||
}
|
||||
@@ -91,7 +103,15 @@ func AsymDecryptArmored(PrivateKey string, data []byte) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption b64 failed: %w", err)
|
||||
}
|
||||
decrypted, err := helper.DecryptBinaryMessageArmored(string(priv), nil, string(data))
|
||||
privKey, err := crypto.NewKey(priv)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption key parse failed: %w", err)
|
||||
}
|
||||
armoredPriv, err := privKey.Armor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption key armor failed: %w", err)
|
||||
}
|
||||
decrypted, err := helper.DecryptBinaryMessageArmored(armoredPriv, nil, string(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption failed: %w", err)
|
||||
}
|
||||
@@ -134,7 +154,7 @@ func AsymDecryptArmored(PrivateKey string, data []byte) ([]byte, error) {
|
||||
return DecryptedMessage, err
|
||||
}
|
||||
*/
|
||||
func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessage, error) {
|
||||
func encryptMessage(key []byte, message *crypto.PlainMessage) (*crypto.PGPMessage, error) {
|
||||
publicKeyRing, err := createPublicKeyRing(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -148,8 +168,8 @@ func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessag
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
func decryptMessage(privateKey string, passphrase []byte, ciphertext *crypto.PGPMessage) (*crypto.PlainMessage, error) {
|
||||
privateKeyObj, err := crypto.NewKeyFromArmored(privateKey)
|
||||
func decryptMessage(privateKey []byte, passphrase []byte, ciphertext *crypto.PGPMessage) (*crypto.PlainMessage, error) {
|
||||
privateKeyObj, err := crypto.NewKey(privateKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gopenpgp: unable to parse the private key: %w", err)
|
||||
}
|
||||
@@ -174,8 +194,8 @@ func decryptMessage(privateKey string, passphrase []byte, ciphertext *crypto.PGP
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func createPublicKeyRing(publicKey string) (*crypto.KeyRing, error) {
|
||||
publicKeyObj, err := crypto.NewKeyFromArmored(publicKey)
|
||||
func createPublicKeyRing(publicKey []byte) (*crypto.KeyRing, error) {
|
||||
publicKeyObj, err := crypto.NewKey(publicKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gopenpgp: unable to parse public key: %w", err)
|
||||
}
|
||||
@@ -205,7 +225,7 @@ func AsymEncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string,
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption and sign b64 failed: %w", err)
|
||||
}
|
||||
ciphertext, signature, err := encryptAndSignMessage(string(pub), string(priv), crypto.NewPlainMessage(data))
|
||||
ciphertext, signature, err := encryptAndSignMessage(pub, priv, crypto.NewPlainMessage(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message encryption failed: %w", err)
|
||||
}
|
||||
@@ -223,14 +243,14 @@ func AsymDecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey strin
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption and sign b64 failed: %w", err)
|
||||
}
|
||||
DecryptedMessage, err = decryptAndCheckMessage(string(pub), string(priv), crypto.NewPGPMessage(data), crypto.NewPGPSignature(Signature))
|
||||
DecryptedMessage, err = decryptAndCheckMessage(pub, priv, crypto.NewPGPMessage(data), crypto.NewPGPSignature(Signature))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Message decryption and sign failed: %w", err)
|
||||
}
|
||||
return DecryptedMessage, err
|
||||
}
|
||||
|
||||
func encryptAndSignMessage(pub string, priv string, message *crypto.PlainMessage) (*crypto.PGPMessage, []byte, error) {
|
||||
func encryptAndSignMessage(pub []byte, priv []byte, message *crypto.PlainMessage) (*crypto.PGPMessage, []byte, error) {
|
||||
var privateKeyObj, unlockedKeyObj *crypto.Key
|
||||
var privateKeyRing *crypto.KeyRing
|
||||
publicKeyRing, err := createPublicKeyRing(pub)
|
||||
@@ -238,11 +258,7 @@ func encryptAndSignMessage(pub string, priv string, message *crypto.PlainMessage
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("gopenpgp: unable to encrypt message")
|
||||
}
|
||||
|
||||
if privateKeyObj, err = crypto.NewKeyFromArmored(priv); err != nil {
|
||||
if privateKeyObj, err = crypto.NewKey(priv); err != nil {
|
||||
return nil, nil, fmt.Errorf("gopenpgp: unable to parse private key")
|
||||
}
|
||||
|
||||
@@ -267,7 +283,7 @@ func encryptAndSignMessage(pub string, priv string, message *crypto.PlainMessage
|
||||
return ciphertext, signature.GetBinary(), nil
|
||||
}
|
||||
|
||||
func decryptAndCheckMessage(pub string, priv string, message *crypto.PGPMessage, signature *crypto.PGPSignature) ([]byte, error) {
|
||||
func decryptAndCheckMessage(pub []byte, priv []byte, message *crypto.PGPMessage, signature *crypto.PGPSignature) ([]byte, error) {
|
||||
var privateKeyObj, unlockedKeyObj *crypto.Key
|
||||
var privateKeyRing *crypto.KeyRing
|
||||
publicKeyRing, err := createPublicKeyRing(pub)
|
||||
@@ -275,11 +291,7 @@ func decryptAndCheckMessage(pub string, priv string, message *crypto.PGPMessage,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gopenpgp: unable to encrypt message")
|
||||
}
|
||||
|
||||
if privateKeyObj, err = crypto.NewKeyFromArmored(priv); err != nil {
|
||||
if privateKeyObj, err = crypto.NewKey(priv); err != nil {
|
||||
return nil, fmt.Errorf("gopenpgp: unable to parse private key")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user