New functions for flutter openpgp compatibility

This commit is contained in:
ycc
2022-11-20 17:52:27 +01:00
parent 7fe7892764
commit 2516b41597
2 changed files with 181 additions and 0 deletions

View File

@ -192,3 +192,110 @@ func createPublicKeyRing(publicKey string) (*crypto.KeyRing, error) {
return publicKeyRing, nil
}
func AsymEncryptAndSign2(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) ([]byte, []byte, error) {
pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey)
if err != nil {
log.Error().Msg("Message encryption and sign b64 failed")
}
priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey)
if err != nil {
log.Error().Msg("Message encryption and sign b64 failed")
}
ciphertext, signature, err := encryptAndSignMessage(string(pub), string(priv), crypto.NewPlainMessage(data))
if err != nil {
log.Error().Msg("Message encryption failed")
return nil, nil, err
}
return ciphertext.GetBinary(), signature, err
}
func AsymDecryptAndCheck2(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) {
priv, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey)
if err != nil {
log.Error().Msg("Message decryption and sign b64 failed")
}
pub, err := base64.StdEncoding.DecodeString(MyContactPublicKey)
if err != nil {
log.Error().Msg("Message decryption and sign b64 failed")
}
DecryptedMessage, err = decryptAndCheckMessage(string(pub), string(priv), crypto.NewPGPMessage(data), crypto.NewPGPSignature(Signature))
if err != nil {
log.Error().Msg("Message decryption and sign failed")
}
return DecryptedMessage, err
}
func encryptAndSignMessage(pub string, priv string, message *crypto.PlainMessage) (*crypto.PGPMessage, []byte, error) {
var privateKeyObj, unlockedKeyObj *crypto.Key
var privateKeyRing *crypto.KeyRing
publicKeyRing, err := createPublicKeyRing(pub)
if err != nil {
return nil, nil, err
}
if err != nil {
return nil, nil, errors.Wrap(err, "gopenpgp: unable to encrypt message")
}
if privateKeyObj, err = crypto.NewKeyFromArmored(priv); err != nil {
return nil, nil, errors.Wrap(err, "gopenpgp: unable to parse private key")
}
if unlockedKeyObj, err = privateKeyObj.Unlock(nil); err != nil {
return nil, nil, errors.Wrap(err, "gopenpgp: unable to unlock key")
}
defer unlockedKeyObj.ClearPrivateParams()
if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil {
return nil, nil, errors.Wrap(err, "gopenpgp: unable to create private keyring")
}
ciphertext, err := publicKeyRing.Encrypt(message, nil)
if err != nil {
return nil, nil, errors.Wrap(err, "gopenpgp: unable to encrypt message")
}
signature, err := privateKeyRing.SignDetached(message)
if err != nil {
return nil, nil, errors.Wrap(err, "gopenpgp: unable to encrypt message")
}
return ciphertext, signature.GetBinary(), nil
}
func decryptAndCheckMessage(pub string, priv string, message *crypto.PGPMessage, signature *crypto.PGPSignature) ([]byte, error) {
var privateKeyObj, unlockedKeyObj *crypto.Key
var privateKeyRing *crypto.KeyRing
publicKeyRing, err := createPublicKeyRing(pub)
if err != nil {
return nil, err
}
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to encrypt message")
}
if privateKeyObj, err = crypto.NewKeyFromArmored(priv); err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to parse private key")
}
if unlockedKeyObj, err = privateKeyObj.Unlock(nil); err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to unlock key")
}
defer unlockedKeyObj.ClearPrivateParams()
if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to create private keyring")
}
plainmessage, err := privateKeyRing.Decrypt(message, nil, 0)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to decrypt message")
}
err = publicKeyRing.VerifyDetached(plainmessage, signature, crypto.GetUnixTime())
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to check message signature")
}
return plainmessage.GetBinary(), nil
}