50 lines
894 B
Go
50 lines
894 B
Go
package shadow
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"io"
|
|
"math/rand"
|
|
)
|
|
|
|
type RandReader struct {
|
|
io.Reader
|
|
}
|
|
|
|
func (r RandReader) Read(p []byte) (n int, err error) {
|
|
buf := make([]byte, 32)
|
|
randBytes := md5.Sum(RandomByte(16))
|
|
copy(buf[0:], randBytes[:])
|
|
|
|
preHashData := bytes.NewBuffer(randBytes[:])
|
|
sum := md5.Sum([]byte(HandshakePassword))
|
|
preHashData.Write(sum[:])
|
|
hash := md5.Sum(preHashData.Bytes())
|
|
copy(buf[16:], hash[:])
|
|
copy(p, buf)
|
|
return 32, nil
|
|
}
|
|
|
|
func RandomByte(size int) []byte {
|
|
buf := make([]byte, size)
|
|
for i := 0; i < size; i++ {
|
|
buf[i] = byte(rand.Intn(255))
|
|
}
|
|
return buf
|
|
}
|
|
|
|
func VerifyKey(p []byte, key string) bool {
|
|
if len(p) != 32 {
|
|
return false
|
|
}
|
|
buf := make([]byte, 16)
|
|
copy(buf, p[0:16])
|
|
buffer := bytes.NewBuffer(buf)
|
|
sum := md5.Sum([]byte(key))
|
|
buffer.Write(sum[:])
|
|
|
|
hash := md5.Sum(buffer.Bytes())
|
|
|
|
return bytes.Equal(hash[:], p[16:])
|
|
}
|