Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Entropy-Narcissist/Go/entropy-narcissist.go
Normal file
35
Task/Entropy-Narcissist/Go/entropy-narcissist.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_, src, _, _ := runtime.Caller(0)
|
||||
fmt.Println("Source file entropy:", entropy(src))
|
||||
fmt.Println("Binary file entropy:", entropy(os.Args[0]))
|
||||
}
|
||||
|
||||
func entropy(file string) float64 {
|
||||
d, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
var f [256]float64
|
||||
for _, b := range d {
|
||||
f[b]++
|
||||
}
|
||||
hm := 0.
|
||||
for _, c := range f {
|
||||
if c > 0 {
|
||||
hm += c * math.Log2(c)
|
||||
}
|
||||
}
|
||||
l := float64(len(d))
|
||||
return math.Log2(l) - hm/l
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue