mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
42 lines
683 B
Go
42 lines
683 B
Go
package common
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
)
|
|
|
|
type Runnable interface {
|
|
Run() error
|
|
Close() error
|
|
}
|
|
|
|
func SHA224String(password string) string {
|
|
hash := sha256.New224()
|
|
hash.Write([]byte(password))
|
|
val := hash.Sum(nil)
|
|
str := ""
|
|
for _, v := range val {
|
|
str += fmt.Sprintf("%02x", v)
|
|
}
|
|
return str
|
|
}
|
|
|
|
const (
|
|
KiB = 1024
|
|
MiB = KiB * 1024
|
|
GiB = MiB * 1024
|
|
)
|
|
|
|
func HumanFriendlyTraffic(bytes int) string {
|
|
if bytes <= KiB {
|
|
return fmt.Sprintf("%d B", bytes)
|
|
}
|
|
if bytes <= MiB {
|
|
return fmt.Sprintf("%.2f KiB", float32(bytes)/KiB)
|
|
}
|
|
if bytes <= GiB {
|
|
return fmt.Sprintf("%.2f MiB", float32(bytes)/MiB)
|
|
}
|
|
return fmt.Sprintf("%.2f TiB", float32(bytes)/GiB)
|
|
}
|