mirror of
https://github.com/safing/portmaster
synced 2025-09-16 01:39:41 +00:00
Work on portmaster restructuring
This commit is contained in:
parent
3990790f17
commit
62b1c03edc
13 changed files with 349 additions and 112 deletions
43
process/executable.go
Normal file
43
process/executable.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
|
||||
|
||||
package process
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"encoding/hex"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// GetExecHash returns the hash of the executable with the given algorithm.
|
||||
func (p *Process) GetExecHash(algorithm string) (string, error) {
|
||||
sum, ok := p.ExecHashes[algorithm]
|
||||
if ok {
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
var hasher hash.Hash
|
||||
switch algorithm {
|
||||
case "md5":
|
||||
hasher = crypto.MD5.New()
|
||||
case "sha1":
|
||||
hasher = crypto.SHA1.New()
|
||||
case "sha256":
|
||||
hasher = crypto.SHA256.New()
|
||||
}
|
||||
|
||||
file, err := os.Open(p.Path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = io.Copy(hasher, file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sum = hex.EncodeToString(hasher.Sum(nil))
|
||||
p.ExecHashes[algorithm] = sum
|
||||
return sum, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue