Skip to content

Commit

Permalink
EncodeBase64 returns no error
Browse files Browse the repository at this point in the history
  • Loading branch information
yosida95 committed Jan 24, 2014
1 parent 32e7b15 commit 453512c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
5 changes: 1 addition & 4 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ func (assoc *Association) Sign(msg Message, signed []string) (err error) {

mac := hmac.New(assoc.assocType.hashFunc, assoc.secret)
mac.Write(kv)
sig, err := EncodeBase64(mac.Sum(nil))
if err != nil {
return
}
sig := EncodeBase64(mac.Sum(nil))

msg.AddArg(
NewMessageKey(msg.GetOpenIDNamespace(), "signed"),
Expand Down
2 changes: 1 addition & 1 deletion association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestCreateAssociation(t *testing.T) {
kv, err := msg.ToKeyValue([]string{"openid.mode", "openid.ns"})
if assert.Nil(t, err) {
mac.Write(kv)
expected, err := EncodeBase64(mac.Sum(nil))
expected := EncodeBase64(mac.Sum(nil))
if assert.Nil(t, err) {
assert.Equal(t, sig.String(), string(expected))
}
Expand Down
19 changes: 6 additions & 13 deletions provider/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,7 @@ func (s *AssociateSession) buildResponse() (res *OpenIDResponse, err error) {
)

if s.request.sessionType.Name() == gopenid.SESSION_NO_ENCRYPTION.Name() {
var macKey []byte
macKey, err = gopenid.EncodeBase64(assoc.GetSecret())
if err != nil {
return
}
macKey := gopenid.EncodeBase64(assoc.GetSecret())

res.AddArg(
gopenid.NewMessageKey(res.GetNamespace(), "mac_key"),
Expand All @@ -255,11 +251,7 @@ func (s *AssociateSession) buildResponse() (res *OpenIDResponse, err error) {
}
)

var serverPublic []byte
serverPublic, err = gopenid.EncodeBase64(key.PublicKey.Y.Bytes())
if err != nil {
return
}
serverPublic := gopenid.EncodeBase64(key.PublicKey.Y.Bytes())
res.AddArg(
gopenid.NewMessageKey(res.GetNamespace(), "dh_server_public"),
gopenid.MessageValue(serverPublic),
Expand All @@ -272,13 +264,14 @@ func (s *AssociateSession) buildResponse() (res *OpenIDResponse, err error) {
h.Write(shared.ZZ.Bytes())
hashedShared := h.Sum(nil)

encMacKey := make([]byte, s.request.assocType.GetSecretSize())
dhMacKey := make([]byte, s.request.assocType.GetSecretSize())
for i := 0; i < s.request.assocType.GetSecretSize(); i++ {
encMacKey[i] = hashedShared[i] ^ secret[i]
dhMacKey[i] = hashedShared[i] ^ secret[i]
}
dhMacKey = gopenid.EncodeBase64(dhMacKey)
res.AddArg(
gopenid.NewMessageKey(res.GetNamespace(), "dh_mac_key"),
gopenid.MessageValue(encMacKey),
gopenid.MessageValue(dhMacKey),
)
}

Expand Down
7 changes: 4 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ func GenerateNonce(now time.Time) MessageValue {
return MessageValue(ts + salt)
}

func EncodeBase64(input []byte) ([]byte, error) {
func EncodeBase64(input []byte) (b []byte) {
encoded := bytes.NewBuffer(nil)
encoder := base64.NewEncoder(base64.StdEncoding, encoded)

encoder.Write(input)
encoder.Close()

return ioutil.ReadAll(encoded)
b, _ = ioutil.ReadAll(encoded)
return
}

func DecodeBase64(encoded []byte) (buf []byte, err error) {
Expand All @@ -54,7 +55,7 @@ func DecodeBase64(encoded []byte) (buf []byte, err error) {
return
}

func IntToBase64(i *big.Int) (output []byte, err error) {
func IntToBase64(i *big.Int) (output []byte) {
return EncodeBase64(i.Bytes())
}

Expand Down
6 changes: 2 additions & 4 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ func init() {

func TestSwitchWithBase64AndInt(t *testing.T) {
for _, testCase := range switchWithBase64AndIntCases {
b64, err := IntToBase64(testCase.int_)
if assert.Nil(t, err) {
assert.Equal(t, testCase.b64, string(b64))
}
b64 := IntToBase64(testCase.int_)
assert.Equal(t, testCase.b64, string(b64))

int_, err := Base64ToInt([]byte(testCase.b64))
if assert.Nil(t, err) {
Expand Down

0 comments on commit 453512c

Please sign in to comment.