Skip to content

Commit baeee21

Browse files
committed
init commit part2
Signed-off-by: David Liu <[email protected]>
1 parent a2ea789 commit baeee21

File tree

8 files changed

+300
-0
lines changed

8 files changed

+300
-0
lines changed

common/crypto/random.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package crypto
18+
19+
import (
20+
"crypto/rand"
21+
22+
"github.com/pkg/errors"
23+
)
24+
25+
const (
26+
// NonceSize is the default NonceSize
27+
NonceSize = 24
28+
)
29+
30+
// GetRandomBytes returns len random looking bytes
31+
func GetRandomBytes(len int) ([]byte, error) {
32+
key := make([]byte, len)
33+
34+
_, err := rand.Read(key)
35+
if err != nil {
36+
return nil, errors.Wrap(err, "error getting random bytes")
37+
}
38+
39+
return key, nil
40+
}
41+
42+
// GetRandomNonce returns a random byte array of length NonceSize
43+
func GetRandomNonce() ([]byte, error) {
44+
return GetRandomBytes(NonceSize)
45+
}

common/crypto/random_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package crypto
8+
9+
import (
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestGetRandomBytes(t *testing.T) {
16+
_, err := GetRandomBytes(10)
17+
18+
require.NoError(t, err, "GetRandomBytes fails")
19+
}
20+
21+
func TestGetRandomNonce(t *testing.T) {
22+
_, err := GetRandomNonce()
23+
24+
require.NoError(t, err, "GetRandomNonce fails")
25+
}

common/go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/hyperledger/fabric-lib-go/common
2+
3+
go 1.20
4+
5+
require (
6+
github.com/pkg/errors v0.9.1
7+
github.com/stretchr/testify v1.8.4
8+
google.golang.org/protobuf v1.32.0
9+
)
10+
11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
gopkg.in/yaml.v3 v3.0.1 // indirect
15+
)

common/go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
4+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
5+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
9+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
10+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
11+
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
12+
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
16+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

common/util/utils.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package util
8+
9+
import (
10+
"crypto/rand"
11+
"fmt"
12+
"io"
13+
"time"
14+
15+
timestamp "google.golang.org/protobuf/types/known/timestamppb"
16+
)
17+
18+
// GenerateBytesUUID returns a UUID based on RFC 4122 returning the generated bytes
19+
func GenerateBytesUUID() []byte {
20+
uuid := make([]byte, 16)
21+
_, err := io.ReadFull(rand.Reader, uuid)
22+
if err != nil {
23+
panic(fmt.Sprintf("Error generating UUID: %s", err))
24+
}
25+
26+
// variant bits; see section 4.1.1
27+
uuid[8] = uuid[8]&^0xc0 | 0x80
28+
29+
// version 4 (pseudo-random); see section 4.1.3
30+
uuid[6] = uuid[6]&^0xf0 | 0x40
31+
32+
return uuid
33+
}
34+
35+
// GenerateUUID returns a UUID based on RFC 4122
36+
func GenerateUUID() string {
37+
uuid := GenerateBytesUUID()
38+
return idBytesToStr(uuid)
39+
}
40+
41+
// CreateUtcTimestamp returns a Timestamp protobuf in UTC
42+
func CreateUtcTimestamp() *timestamp.Timestamp {
43+
now := time.Now().UTC()
44+
secs := now.Unix()
45+
nanos := int32(now.UnixNano() - (secs * 1000000000))
46+
return &(timestamp.Timestamp{Seconds: secs, Nanos: nanos})
47+
}
48+
49+
func idBytesToStr(id []byte) string {
50+
return fmt.Sprintf("%x-%x-%x-%x-%x", id[0:4], id[4:6], id[6:8], id[8:10], id[10:])
51+
}
52+
53+
// ToChaincodeArgs converts string args to []byte args
54+
func ToChaincodeArgs(args ...string) [][]byte {
55+
bargs := make([][]byte, len(args))
56+
for i, arg := range args {
57+
bargs[i] = []byte(arg)
58+
}
59+
return bargs
60+
}
61+
62+
// ConcatenateBytes is useful for combining multiple arrays of bytes, especially for
63+
// signatures or digests over multiple fields
64+
// This way is more efficient in speed
65+
func ConcatenateBytes(data ...[]byte) []byte {
66+
finalLength := 0
67+
for _, slice := range data {
68+
finalLength += len(slice)
69+
}
70+
result := make([]byte, finalLength)
71+
last := 0
72+
for _, slice := range data {
73+
last += copy(result[last:], slice)
74+
}
75+
return result
76+
}

common/util/utils_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"bytes"
21+
"testing"
22+
"time"
23+
)
24+
25+
func TestUUIDGeneration(t *testing.T) {
26+
uuid := GenerateUUID()
27+
if len(uuid) != 36 {
28+
t.Fatalf("UUID length is not correct. Expected = 36, Got = %d", len(uuid))
29+
}
30+
uuid2 := GenerateUUID()
31+
if uuid == uuid2 {
32+
t.Fatalf("Two UUIDs are equal. This should never occur")
33+
}
34+
}
35+
36+
func TestTimestamp(t *testing.T) {
37+
for i := 0; i < 10; i++ {
38+
t.Logf("timestamp now: %v", CreateUtcTimestamp())
39+
time.Sleep(200 * time.Millisecond)
40+
}
41+
}
42+
43+
func TestToChaincodeArgs(t *testing.T) {
44+
expected := [][]byte{[]byte("foo"), []byte("bar")}
45+
actual := ToChaincodeArgs("foo", "bar")
46+
if len(expected) != len(actual) {
47+
t.Fatalf("Got %v, expected %v", actual, expected)
48+
}
49+
for i := range expected {
50+
if !bytes.Equal(expected[i], actual[i]) {
51+
t.Fatalf("Got %v, expected %v", actual, expected)
52+
}
53+
}
54+
}
55+
56+
func TestConcatenateBytesNormal(t *testing.T) {
57+
first := []byte("first")
58+
second := []byte("second")
59+
third := []byte("third")
60+
61+
result := ConcatenateBytes(first, second, third)
62+
expected := []byte("firstsecondthird")
63+
if !bytes.Equal(result, expected) {
64+
t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result)
65+
}
66+
}
67+
68+
func TestConcatenateBytesNil(t *testing.T) {
69+
first := []byte("first")
70+
second := []byte(nil)
71+
third := []byte("third")
72+
73+
result := ConcatenateBytes(first, second, third)
74+
expected := []byte("firstthird")
75+
if !bytes.Equal(result, expected) {
76+
t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result)
77+
}
78+
}

protoutil/go.sum

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
2+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
4+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
5+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
6+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
8+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
9+
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.2 h1:zL93mhCZbO99pzelinxqx+SahGAVafvbVR6nzRJkXts=
10+
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.2/go.mod h1:2pq0ui6ZWA0cC8J+eCErgnMDCS1kPOEYVY+06ZAK0qE=
11+
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
12+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
13+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
14+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
15+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
16+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
17+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
18+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
19+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
21+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
22+
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
23+
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
24+
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
25+
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
26+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
27+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
28+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
29+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I=
30+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc=
31+
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
32+
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
33+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
34+
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
35+
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
36+
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
37+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
39+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
40+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
41+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

scripts/run-unit-tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
# SPDX-License-Identifier: Apache-2.0
77
#
88

9+
time go test -race -cover ./...
10+
cd common
11+
time go test -race -cover ./...
12+
cd ../protoutil
913
time go test -race -cover ./...

0 commit comments

Comments
 (0)