Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base64 Encode and Decode methods #182

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package script

import (
"bufio"
"bytes"
"container/ring"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -759,6 +761,43 @@ func (p *Pipe) SetError(err error) {
p.err = err
}

// Base64Encode returns the returns the base64 encoding of the
// pipe, or an error.
func (p *Pipe) Base64Encode(enc *base64.Encoding) (string, error) {
if p.Error() != nil {
return "", p.Error()
}
var encodedData bytes.Buffer
encoder := base64.NewEncoder(enc, &encodedData)

_, err := io.Copy(encoder, p)
if err != nil {
p.SetError(err)
return "", err
}
encoder.Close()

return encodedData.String(), nil
}

// Base64Decode returns the string represented by the base64
// pipe, or an error.
func (p *Pipe) Base64Decode(enc *base64.Encoding) (string, error) {
if p.Error() != nil {
return "", p.Error()
}
decoder := base64.NewDecoder(enc, p)

var decodedData bytes.Buffer
_, err := io.Copy(&decodedData, decoder)
if err != nil {
p.SetError(err)
return "", err
}

return decodedData.String(), nil
}

// SHA256Sum returns the hex-encoded SHA-256 hash of the entire contents of the
// pipe, or an error.
func (p *Pipe) SHA256Sum() (string, error) {
Expand Down
55 changes: 55 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package script_test
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -1491,6 +1492,60 @@ func TestCountLines_ReturnsErrorGivenReadErrorOnPipe(t *testing.T) {
}
}

func TestBase64Encode(t *testing.T) {
t.Parallel()
tcs := []struct {
name, input, want string
enc *base64.Encoding
}{
{
name: "for short string",
input: "Hello, world!",
want: "SGVsbG8sIHdvcmxkIQ==",
enc: base64.StdEncoding,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got, err := script.Echo(tc.input).Base64Encode(tc.enc)
if err != nil {
t.Fatal(err)
}
if got != tc.want {
t.Errorf("want %q, got %q", tc.want, got)
}
})
}
}

func TestBase64Decode(t *testing.T) {
t.Parallel()
tcs := []struct {
name, input, want string
enc *base64.Encoding
}{
{
name: "for short string",
input: "SGVsbG8sIHdvcmxkIQ==",
want: "Hello, world!",
enc: base64.StdEncoding,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got, err := script.Echo(tc.input).Base64Decode(tc.enc)
if err != nil {
t.Fatal(err)
}
if got != tc.want {
t.Errorf("want %q, got %q", tc.want, got)
}
})
}
}

func TestSHA256Sum_OutputsCorrectHash(t *testing.T) {
t.Parallel()
tcs := []struct {
Expand Down