Skip to content

Commit

Permalink
chore: added armor decode (#36)
Browse files Browse the repository at this point in the history
* chore: add armor decode 

* chore: updated ioutil
  • Loading branch information
jerson authored Aug 30, 2023
1 parent 961adb2 commit 0b867c7
Show file tree
Hide file tree
Showing 16 changed files with 552 additions and 6 deletions.
34 changes: 33 additions & 1 deletion bridge/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func Call(name string, payload []byte) ([]byte, error) {
output = instance.generate(payload)
case "armorEncode":
output = instance.armorEncode(payload)
case "armorDecode":
output = instance.armorDecode(payload)
case "getPublicKeyMetadata":
output = instance.getPublicKeyMetadata(payload)
case "getPrivateKeyMetadata":
Expand Down Expand Up @@ -265,9 +267,16 @@ func (m instance) armorEncode(payload []byte) []byte {
response := flatbuffers.NewBuilder(0)
request := model.GetRootAsArmorEncodeRequest(payload, 0)

output, err := m.instance.ArmorEncode(request.PacketBytes())
output, err := m.instance.ArmorEncode(request.PacketBytes(), string(request.Type()))
return m._stringResponse(response, output, err)
}
func (m instance) armorDecode(payload []byte) []byte {
response := flatbuffers.NewBuilder(0)
request := model.GetRootAsArmorDecodeRequest(payload, 0)

output, err := m.instance.ArmorDecode(string(request.Message()))
return m._armorDecodeResponse(response, output, err)
}

func (m instance) getPublicKeyMetadata(payload []byte) []byte {
response := flatbuffers.NewBuilder(0)
Expand Down Expand Up @@ -441,6 +450,29 @@ func (m instance) parseCompression(input model.Compression) string {
}
}

func (m instance) _armorDecodeResponse(response *flatbuffers.Builder, output *openpgp.ArmorMetadata, err error) []byte {
if err != nil {
outputOffset := response.CreateString(err.Error())
model.ArmorDecodeResponseStart(response)
model.ArmorDecodeResponseAddError(response, outputOffset)
response.Finish(model.ArmorDecodeResponseEnd(response))
return response.FinishedBytes()
}

bodyOffset := response.CreateByteVector(output.Body)
typeOffset := response.CreateString(output.Type)

model.ArmorMetadataStart(response)
model.ArmorMetadataAddBody(response, bodyOffset)
model.ArmorMetadataAddType(response, typeOffset)
metadata := model.ArmorMetadataEnd(response)

model.ArmorDecodeResponseStart(response)
model.ArmorDecodeResponseAddOutput(response, metadata)
response.Finish(model.ArmorDecodeResponseEnd(response))
return response.FinishedBytes()
}

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.

68 changes: 68 additions & 0 deletions bridge/model/ArmorDecodeResponse.go

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

13 changes: 12 additions & 1 deletion bridge/model/ArmorEncodeRequest.go

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

92 changes: 92 additions & 0 deletions bridge/model/ArmorMetadata.go

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

15 changes: 15 additions & 0 deletions flatbuffers/bridge.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ table GenerateRequest {

table ArmorEncodeRequest {
packet:[ubyte];
type:string;
}

table ArmorDecodeRequest {
message:string;
}

table GetPublicKeyMetadataRequest {
Expand Down Expand Up @@ -297,6 +302,16 @@ table IntResponse {
error:string;
}

table ArmorDecodeResponse {
output:model.ArmorMetadata;
error:string;
}

table ArmorMetadata {
body:[ubyte];
type:string;
}

table KeyPairResponse {
output:model.KeyPair;
error:string;
Expand Down
19 changes: 18 additions & 1 deletion openpgp/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package openpgp

import (
"bytes"
"io/ioutil"
"strings"

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

func (o *FastOpenPGP) ArmorEncode(packet []byte) (string, error) {
func (o *FastOpenPGP) ArmorEncode(packet []byte, messageType string) (string, error) {
buf := bytes.NewBuffer(nil)
writer, err := armor.Encode(buf, messageType, headers)
if err != nil {
Expand All @@ -21,3 +24,17 @@ func (o *FastOpenPGP) ArmorEncode(packet []byte) (string, error) {
}
return buf.String(), nil
}

func (o *FastOpenPGP) ArmorDecode(message string) (*ArmorMetadata, error) {
block, err := armor.Decode(strings.NewReader(message))
if err != nil {
return nil, err
}

output, err := ioutil.ReadAll(block.Body)
if err != nil {
return nil, err
}

return &ArmorMetadata{Body: output, Type: block.Type}, nil
}
20 changes: 19 additions & 1 deletion openpgp/armor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cmFuZG9tIHN0cmluZw==
func TestFastOpenPGP_ArmorEncode(t *testing.T) {

openPGP := NewFastOpenPGP()
output, err := openPGP.ArmorEncode([]byte("random string"))
output, err := openPGP.ArmorEncode([]byte("random string"), messageType)
if err != nil {
t.Fatal(err)
}
Expand All @@ -24,3 +24,21 @@ func TestFastOpenPGP_ArmorEncode(t *testing.T) {
t.Fatal("not same input")
}
}

func TestFastOpenPGP_DecodeEncode(t *testing.T) {

openPGP := NewFastOpenPGP()
output, err := openPGP.ArmorDecode(encoded)
if err != nil {
t.Fatal(err)
}

t.Log("output:", output)

if output.Type != messageType {
t.Fatal("not same type:", output.Type)
}
if string(output.Body) != "random string" {
t.Fatal("not same input")
}
}
Loading

0 comments on commit 0b867c7

Please sign in to comment.