Skip to content

Commit

Permalink
Base 64 encode/decode with padding
Browse files Browse the repository at this point in the history
See https://jira.hyperledger.org/browse/FAB-2491

The problem is that the standard base 64 encoding is with padding,
but fabric-ca was using the raw standard encoding which does not
include padding characters.  The change is to encode with base 64
standard WITH PADDING, and when decoding, try with padding 1st but
if it fails, try without padding.

Change-Id: I43d3456a6ade5cf3f902c0127c1380cbc0dc066d
Signed-off-by: Keith Smith <[email protected]>
  • Loading branch information
Keith Smith committed Feb 27, 2017
1 parent 34ad615 commit 64e22bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ func GetRSAPrivateKey(raw []byte) (*rsa.PrivateKey, error) {

// B64Encode base64 encodes bytes
func B64Encode(buf []byte) string {
return base64.RawStdEncoding.EncodeToString(buf)
return base64.StdEncoding.EncodeToString(buf)
}

// B64Decode base64 decodes a string
func B64Decode(str string) (buf []byte, err error) {
return base64.RawStdEncoding.DecodeString(str)
return base64.StdEncoding.DecodeString(str)
}

// GetDB returns a handle to an established driver-specific database connection
Expand Down
13 changes: 13 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package util

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -374,6 +375,18 @@ func TestMakeFileAbs(t *testing.T) {
makeFileAbs(t, "../c", "/a/b", "/a/c")
}

func TestB64(t *testing.T) {
buf := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
str := B64Encode(buf)
buf2, err := B64Decode(str)
if err != nil {
t.Errorf("Failed base64 decoding standard: %s", err)
}
if !bytes.Equal(buf, buf2) {
t.Error("Failed base64 decoding standard bytes aren't equal")
}
}

func TestGetUser(t *testing.T) {
os.Unsetenv("FABRIC_CA_CLIENT_URL")
viper.BindEnv("url", "FABRIC_CA_CLIENT_URL")
Expand Down

0 comments on commit 64e22bd

Please sign in to comment.