You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Checking data integrity
Verifying the integrity of uploaded objects
Copy page
Download PDF
Verifying the integrity of uploaded objects
You can verify the integrity of uploaded objects using Content-MD5, Content-SHA256, or CRC-64. This topic introduces validation flowcharts, sample codes, and comparisons of different methods.​
Using Content-MD5​
For simple uploads or multipart uploads via TOS APIs, the client calculates the MD5 checksum of the data to be uploaded and passes this value to TOS via request headers or form fields to ensure data integrity.​
Flowchart​
​​​
Code examples​
​
Golang
Java
Python
​
​
package main​
​
import (​
"bytes"​
"context"​
"crypto/md5"​
"encoding/base64"​
​
"github.com/volcengine/ve-tos-golang-sdk/v2/tos"​
)​
​
func main() {​
var (​
accessKey = "your access key"​
secretKey = "your secret key"​
endpoint = "your endpoint"​
region = "your region"​
bucket = "bucketname"​
key = "objectname"​
)​
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))​
if err != nil {​
panic(err)​
}​
​
data := []byte("hello world")​
// Calculate the MD5 checksum​
hash := md5.New()​
hash.Write(data)​
contentMD5 := base64.StdEncoding.EncodeToString(hash.Sum(nil))​
​
// Upload object with the specified Content-MD5 header​
input := &tos.PutObjectV2Input{}​
input.Bucket = bucket​
input.Key = key​
input.Content = bytes.NewReader(data)​
input.ContentMD5 = contentMD5​
_, err = client.PutObjectV2(context.Background(), input)​
if err != nil {​
panic(err)​
}​
}​
​
​
Using Content-SHA256​
This process for Content-SHA256 is the same as Content-MD5.​
Flowchart​
​​​
Code examples​
​
Golang
Java
Python
​
​
package main​
​
import (​
"bytes"​
"context"​
"crypto/sha256"​
"encoding/hex"​
​
"github.com/volcengine/ve-tos-golang-sdk/v2/tos"​
)​
​
​
func main() {​
var (​
accessKey = "your access key"​
secretKey = "your secret key"​
endpoint = "your endpoint"​
region = "your region"​
bucket = "bucketname"​
key = "objectname"​
)​
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))​
if err != nil {​
panic(err)​
}​
​
data := []byte("hello world")​
// Calculate the SHA-256 checksum​
hash := sha256.New()​
hash.Write(data)​
contentSHA256 := hex.EncodeToString(hash.Sum(nil))​
​
// Upload object with the specified Content-SHA256 header​
input := &tos.PutObjectV2Input{}​
input.Bucket = bucket​
input.Key = key​
input.Content = bytes.NewReader(data)​
input.ContentSHA256 = contentSHA256​
_, err = client.PutObjectV2(context.Background(), input)​
if err != nil {​
panic(err)​
}​
}​
​
​
Using CRC-64​
For simple or multipart uploads via the TOS APIs, the client first calculates a CRC-64 checksum of the original data. Then, TOS calculates a CRC-64 checksum of the received data. Then, the server compares the two values to ensure the integrity of the uploaded data.​
For multipart uploads, in addition to the aforementioned check, a composite checksum is calculated based on the individual checksums of each part. This value is then compared to the checksum of the entire object that the server returns to ensure the integrity of the final stored object.​
Flowchart​
​​​
Code examples​
​
Golang
Python
​
​
package main​
​
import (​
"bytes"​
"context"​
"hash"​
"io"​
​
"github.com/volcengine/ve-tos-golang-sdk/v2/tos"​
)​
​
type crc64Reader struct {​
r io.Reader​
h hash.Hash64​
}​
​
func (cr *crc64Reader) Read(p []byte) (n int, err error) {​
n, err = cr.r.Read(p)​
cr.h.Write(p[:n])​
return n, err​
}​
​
func main() {​
var (​
accessKey = "your access key"​
secretKey = "your secret key"​
endpoint = "your endpoint"​
region = "your region"​
bucket = "bucketname"​
key = "objectname"​
)​
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))​
if err != nil {​
panic(err)​
}​
​
resp, err := client.CreateMultipartUploadV2(context.Background(), &tos.CreateMultipartUploadV2Input{​
Bucket: bucket,​
Key: key,​
})​
if err != nil {​
panic(err)​
}​
​
uploadID := resp.UploadID​
parts := make([]tos.UploadedPartV2, 0, 3)​
​
var crc64Final uint64​
data := make([]byte, 1024*1024*5)​
for i := 1; i <= 2; i++ {​
input := &tos.UploadPartV2Input{}​
input.Bucket = bucket​
input.Key = key​
input.UploadID = uploadID​
input.PartNumber = i​
​
crc64Hash := tos.NewCRC(tos.DefaultCrcTable(), 0)​
input.Content = &crc64Reader{​
r: bytes.NewReader(data),​
h: crc64Hash,​
}​
output, err := client.UploadPartV2(context.Background(), input)​
if err != nil {​
panic(err)​
}​
​
crc64Once := crc64Hash.Sum64()​
if output.HashCrc64ecma != crc64Once {​
panic("crc64 not match")​
}​
​
crc64Final = tos.CRC64Combine(crc64Final, crc64Once, uint64(len(data)))​
​
parts = append(parts, tos.UploadedPartV2{​
PartNumber: input.PartNumber,​
ETag: output.ETag,​
})​
}​
​
output, err := client.CompleteMultipartUploadV2(context.Background(), &tos.CompleteMultipartUploadV2Input{​
Bucket: bucket,​
Key: key,​
UploadID: uploadID,​
Parts: parts,​
})​
​
if err != nil {​
panic(err)​
}​
​
if output.HashCrc64ecma != crc64Final {​
panic("crc64 not match")​
}​
}​
​
​
Comparison of the different validation methods​
​
Validation method​
Content-MD5​
Content-SHA256​
CRC64​
Use cases​
  • When the MD5 checksum of the data is known beforehand.​
  • When the data to be uploaded is small in size, typically under 10 MB. Calculating the checksum for larger data requires a higher cost.​
  • When data validation fails and you do not want to save the object to TOS.​
  • When the SHA-256 checksum of the data is known beforehand.​
  • When the data to be uploaded is small in size, typically under 10 MB. Calculating the checksum for larger data requires a higher cost.​
  • When data validation fails and you do not want to save the object to TOS.​
  • When you prefer a more secure hash algorithm for data validation.​
  • The CRC-64 checksum is calculated when data is uploaded to TOS, eliminating the need for a separate calculation.​
  • This method is particularly useful for multipart uploads. Use it to calculate the full object checksum and validate it against the provided value to ensure the integrity of the final downloaded object.​
Disadvantages​
  • Requires the pre-calculation of the MD5 checksum, which incurs a cost.​
  • The MD5 algorithm is less secure than SHA-256.​
  • Requires the pre-calculation of the SHA-256 checksum, which incurs a cost.​
  • The SHA-256 algorithm is less efficient than MD5.​
Data validation is performed on the client side, and the object has been saved to TOS. If validation fails, additional cleanup actions are required, such as overwriting or deleting the object.​
Supported APIs​
  • PutObject​
  • UploadPart​
  • PutObject​
  • PostObject​
  • UploadPart​
Recommendations​
  • To avoid additional computation costs, use CRC-64 for data validation.​
  • Use Content-SHA256 when you prefer a more secure hash algorithm and do not want to save the object to TOS if the data is inconsistent. Otherwise, use Content-MD5.​
​
​
​
​
​
Last updated: 2026.10.10 17:55:21