lychee-slicer/asar/integrity.go
2024-10-20 23:04:38 +02:00

42 lines
809 B
Go

package asar
import (
"crypto"
"fmt"
)
const (
ALGORITHM = "SHA256"
BLOCK_SIZE = 4 * 1024 * 1024
)
type FileIntegrity struct {
Algorithm string `json:"algorithm"`
Hash string `json:"hash"`
BlockSize int `json:"blockSize"`
Blocks []string `json:"blocks"`
}
func getFileIntegrity(data []byte) *FileIntegrity {
blockHashes := []string{}
for i := 0; i < len(data); i += BLOCK_SIZE {
end := i + BLOCK_SIZE
if end > len(data) {
end = len(data)
}
blockHashes = append(blockHashes, hashBlock(data[i:end]))
}
return &FileIntegrity{
Algorithm: ALGORITHM,
Hash: hashBlock(data),
BlockSize: BLOCK_SIZE,
Blocks: blockHashes,
}
}
func hashBlock(block []byte) string {
hash := crypto.SHA256.New()
hash.Write(block)
return fmt.Sprintf("%x", hash.Sum(nil))
}