Skip to content

Commit

Permalink
Merge pull request #8 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
feature(security): md5 sort hashmap to sorted message
  • Loading branch information
Matrix-X authored Jun 4, 2022
2 parents c0943e0 + 4595052 commit 4d18379
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
23 changes: 23 additions & 0 deletions security/sign/md5Signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"sort"
)
Expand Down Expand Up @@ -63,3 +64,25 @@ func (signer *MD5Signer) KSortDataToMessage(data *object.StringMap) (msg string,

return msg, err
}

func (signer *MD5Signer) KSortObjectToMessage(data *object.HashMap) (msg string, err error) {

// k sort
var keys []string
mapData := *data
for k := range mapData {
keys = append(keys, k)
}
sort.Strings(keys)

// join
for _, k := range keys {
//fmt.Println("key:", k, "Value:", mapData[k])
msg = msg + fmt.Sprintf("%s=%v&", k, mapData[k])
}

// omit last &
msg = msg[0 : len(msg)-1]

return msg, err
}
46 changes: 46 additions & 0 deletions security/sign/md5signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,55 @@ package sign

import (
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_MD5_KSortDataToMessage(t *testing.T) {

arrayData := &object.StringMap{
"b": "2",
"a": "1",
"c": "3",
}

signer, err := NewMD5Signer("AppID+AppSecret")
if err != nil {
t.Error(err)
}

sortMsg, err := signer.KSortDataToMessage(arrayData)
if err != nil {
t.Error(err)
}

assert.EqualValues(t, "a=1&b=2&c=3", sortMsg)

}

func Test_MD5_KSortObjectToMessage(t *testing.T) {

arrayData := &object.HashMap{
"b": 2,
"a": "1",
"c": 3,
}

signer, err := NewMD5Signer("AppID+AppSecret")
if err != nil {
t.Error(err)
}

sortMsg, err := signer.KSortObjectToMessage(arrayData)
//fmt.Dump(sortMsg)
if err != nil {
t.Error(err)
}

assert.EqualValues(t, "a=1&b=2&c=3", sortMsg)

}

func Test_MD5_VerifySignedMessage(t *testing.T) {

arrayData := &object.StringMap{
Expand Down

0 comments on commit 4d18379

Please sign in to comment.