Skip to content

Commit edec85b

Browse files
authored
Merge pull request #17 from ethan-lowman-dd/ethan.lowman/export-decode
2 parents 7438362 + f258f0c commit edec85b

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

dsse/sign.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ type Envelope struct {
3131
Signatures []Signature `json:"signatures"`
3232
}
3333

34+
/*
35+
DecodeB64Payload returns the serialized body, decoded
36+
from the envelope's payload field. A flexible
37+
decoder is used, first trying standard base64, then
38+
URL-encoded base64.
39+
*/
40+
func (e *Envelope) DecodeB64Payload() ([]byte, error) {
41+
return b64Decode(e.Payload)
42+
}
43+
3444
/*
3545
Signature represents a generic in-toto signature that contains the identifier
3646
of the key which was used to create the signature.

dsse/sign_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func TestEcdsaSign(t *testing.T) {
331331
assert.Equal(t, acceptedKeys[0].KeyID, keyID, "unexpected keyid")
332332
}
333333

334-
func TestB64Decode(t *testing.T) {
334+
func TestDecodeB64Payload(t *testing.T) {
335335
var want = make([]byte, 256)
336336
for i := range want {
337337
want[i] = byte(i)
@@ -342,23 +342,35 @@ func TestB64Decode(t *testing.T) {
342342
var b64StdErr = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w"
343343

344344
t.Run("Standard encoding", func(t *testing.T) {
345-
got, err := b64Decode(b64Std)
345+
env := &Envelope{
346+
Payload: b64Std,
347+
}
348+
got, err := env.DecodeB64Payload()
346349
assert.Nil(t, err, "unexpected error")
347350
assert.Equal(t, want, got, "wrong data")
348351
})
349352
t.Run("URL encoding", func(t *testing.T) {
350-
got, err := b64Decode(b64Url)
353+
env := &Envelope{
354+
Payload: b64Url,
355+
}
356+
got, err := env.DecodeB64Payload()
351357
assert.Nil(t, err, "unexpected error")
352358
assert.Equal(t, want, got, "wrong data")
353359
})
354360

355361
t.Run("Standard encoding - error", func(t *testing.T) {
356-
got, err := b64Decode(b64StdErr)
362+
env := &Envelope{
363+
Payload: b64StdErr,
364+
}
365+
got, err := env.DecodeB64Payload()
357366
assert.NotNil(t, err, "expected error")
358367
assert.Nil(t, got, "wrong data")
359368
})
360369
t.Run("URL encoding - error", func(t *testing.T) {
361-
got, err := b64Decode(b64UrlErr)
370+
env := &Envelope{
371+
Payload: b64UrlErr,
372+
}
373+
got, err := env.DecodeB64Payload()
362374
assert.NotNil(t, err, "expected error")
363375
assert.Nil(t, got, "wrong data")
364376
})

dsse/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (ev *EnvelopeVerifier) Verify(e *Envelope) ([]AcceptedKey, error) {
4141
}
4242

4343
// Decode payload (i.e serialized body)
44-
body, err := b64Decode(e.Payload)
44+
body, err := e.DecodeB64Payload()
4545
if err != nil {
4646
return nil, err
4747
}

0 commit comments

Comments
 (0)