Fix incorrect sum.

This commit is contained in:
Zhang Huangbin
2023-09-05 16:39:19 +08:00
parent 1ddcc7b3fc
commit e09d83368c
3 changed files with 32 additions and 13 deletions

View File

@@ -74,13 +74,22 @@ err := store.Set("my-key", []byte("my-value"))
Or, call `SetWithChecksum()` to create an object with checksum to
ask S3 server to verify data integrity on server side:
> Currently only 4 algorithm are supported: `CRC32`, `CRC32C`, `SHA1`, `SHA256`.
> Currently 4 algorithm are supported:
> - types.ChecksumAlgorithmCrc32 (`CRC32`)
> - types.ChecksumAlgorithmCrc32c (`CRC32C`)
> - types.ChecksumAlgorithmSha1 (`SHA1`)
> - types.ChecksumAlgorithmSha256 (`SHA256`)
>
> For more information, see [PutObjectInput](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3#PutObjectInput).
```go
val := []byte("my-value")
sha256sum := sha256.New().Sum256(val)
checksum := map[string][]byte{"SHA256": sha256sum}
// import "github.com/aws/aws-sdk-go-v2/service/s3/types"
checksum = map[types.ChecksumAlgorithm][]byte{
types.ChecksumAlgorithmSha256: sha256sum,
}
err := store.SetWithChecksum("my-key", []byte("my-value"), checksum)
```

View File

@@ -2,20 +2,26 @@ package s3
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
// Additional methods for S3, but not required by gofiber Storage interface.
// SetWithChecksum sets key with value and checksum.
// Key of `checksum` map is algorithm in upper cases, value is the checksum.
// Currently only 4 algorithm are supported: `CRC32`, `CRC32C`, `SHA1`, `SHA256`.
// Currently only 4 algorithm are supported:
// - types.ChecksumAlgorithmCrc32 (`CRC32`)
// - types.ChecksumAlgorithmCrc32c (`CRC32C`)
// - types.ChecksumAlgorithmSha1 (`SHA1`)
// - types.ChecksumAlgorithmSha256 (`SHA256`)
//
// For more information, see [PutObjectInput](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3#PutObjectInput).
func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string][]byte) error {
func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[types.ChecksumAlgorithm][]byte) error {
if len(key) <= 0 {
return nil
}
@@ -31,14 +37,14 @@ func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string][]
for alg, sum := range checksum {
switch alg {
case "CRC32":
poi.ChecksumCRC32 = aws.String(fmt.Sprintf("%x", sum))
case "CRC32C":
poi.ChecksumCRC32C = aws.String(fmt.Sprintf("%x", sum))
case "SHA1":
poi.ChecksumSHA1 = aws.String(fmt.Sprintf("%x", sum))
case "SHA256":
poi.ChecksumSHA256 = aws.String(fmt.Sprintf("%x", sum))
case types.ChecksumAlgorithmCrc32:
poi.ChecksumCRC32 = aws.String(hex.EncodeToString(sum))
case types.ChecksumAlgorithmCrc32c:
poi.ChecksumCRC32C = aws.String(hex.EncodeToString(sum))
case types.ChecksumAlgorithmSha1:
poi.ChecksumSHA1 = aws.String(hex.EncodeToString(sum))
case types.ChecksumAlgorithmSha256:
poi.ChecksumSHA256 = aws.String(hex.EncodeToString(sum))
default:
return fmt.Errorf("invalid checksum algorithm: %s", alg)
}

View File

@@ -4,6 +4,7 @@ import (
"crypto/sha256"
"testing"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/stretchr/testify/require"
)
@@ -12,9 +13,12 @@ func Test_S3_SetWithChecksum(t *testing.T) {
key = "john"
val = []byte("doe")
sha256sum = sha256.New().Sum(val)
checksum = map[types.ChecksumAlgorithm][]byte{
types.ChecksumAlgorithmSha256: sha256sum,
}
)
err := testStore.SetWithChecksum(key, val, map[string][]byte{"SHA256": sha256sum})
err := testStore.SetWithChecksum(key, val, checksum)
require.NoError(t, err)
result, err := testStore.Get(key)