Skip to content

Commit

Permalink
Merge pull request #19 from Producement/armor_encode
Browse files Browse the repository at this point in the history
Added Armor encode input
  • Loading branch information
jerson authored May 12, 2022
2 parents 92cb414 + 7fae22f commit ec85035
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 1 deletion.
13 changes: 13 additions & 0 deletions bridge/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func Call(name string, payload []byte) ([]byte, error) {
output = instance.encryptSymmetricBytes(payload)
case "generate":
output = instance.generate(payload)
case "armorEncode":
output = instance.armorEncode(payload)
default:
return nil, fmt.Errorf("not implemented: %s", name)
}
Expand Down Expand Up @@ -374,6 +376,17 @@ func (m instance) parseCompression(input model.Compression) string {
}
}

func (m instance) armorEncode(payload []byte) []byte {
response := flatbuffers.NewBuilder(0)
request := model.GetRootAsArmorEncodeRequest(payload, 0)

output, err := m.instance.ArmorEncode(request.PacketBytes())
if err != nil {
return m._stringResponse(response, output, err)
}
return m._stringResponse(response, output, nil)
}

func (m instance) _keyPairResponse(response *flatbuffers.Builder, output *openpgp.KeyPair, err error) []byte {
if err != nil {
outputOffset := response.CreateString(err.Error())
Expand Down
52 changes: 52 additions & 0 deletions bridge/model/ArmorDecodeRequest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions bridge/model/ArmorEncodeRequest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion flatbuffers/bridge.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ table GenerateRequest {
options:model.Options;
}

table ArmorEncodeRequest {
packet:[ubyte];
}

/// KeyOptions collects a number of parameters along with sensible defaults.
table KeyOptions {
/// Hash is the default hash function to be used.
Expand Down Expand Up @@ -255,4 +259,4 @@ table PrivateKeyMetadata {
key_id_numeric:string;
is_sub_key:bool;
encrypted:bool;
}
}
23 changes: 23 additions & 0 deletions openpgp/armor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package openpgp

import (
"bytes"
"github.com/ProtonMail/go-crypto/openpgp/armor"
)

func (o FastOpenPGP) ArmorEncode(packet []byte) (string, error) {
buf := bytes.NewBuffer(nil)
writer, err := armor.Encode(buf, messageType, headers)
if err != nil {
return "", err
}
_, err = writer.Write(packet)
if err != nil {
return "", err
}
err = writer.Close()
if err != nil {
return "", err
}
return buf.String(), nil
}
27 changes: 27 additions & 0 deletions openpgp/armor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package openpgp

import (
"testing"
)

var encoded = `-----BEGIN PGP MESSAGE-----
Version: openpgp-mobile
cmFuZG9tIHN0cmluZw==
=zR7q
-----END PGP MESSAGE-----`

func TestFastOpenPGP_ArmorEncode(t *testing.T) {

openPGP := NewFastOpenPGP()
output, err := openPGP.ArmorEncode([]byte("random string"))
if err != nil {
t.Fatal(err)
}

t.Log("output:", output)

if string(output) != string(encoded) {
t.Fatal("not same input")
}
}

0 comments on commit ec85035

Please sign in to comment.