Files
trojan-go/common/common.go
T
2020-04-01 01:51:31 -04:00

52 lines
863 B
Go

package common
import (
"bufio"
"crypto/sha256"
"fmt"
"io"
)
const (
Version = "v0.0.15"
)
type Runnable interface {
Run() error
Close() error
}
func NewBufReadWriter(rw io.ReadWriter) *bufio.ReadWriter {
return bufio.NewReadWriter(bufio.NewReader(rw), bufio.NewWriter(rw))
}
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 GiB", float32(bytes)/GiB)
}