intial release
This commit is contained in:
parent
abbfcabe8d
commit
dd7e923bff
11 changed files with 1234 additions and 0 deletions
42
asar/integrity.go
Normal file
42
asar/integrity.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
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))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue