github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/client/internal/auth/pow.go (about) 1 package auth 2 3 import ( 4 "bytes" 5 "crypto/sha256" 6 "math/big" 7 "time" 8 9 "github.com/Mrs4s/MiraiGo/binary" 10 ) 11 12 func CalcPow(data []byte) []byte { 13 r := binary.NewReader(data) 14 a := r.ReadByte() 15 typ := r.ReadByte() 16 c := r.ReadByte() 17 ok := r.ReadByte() != 0 18 e := r.ReadUInt16() 19 f := r.ReadUInt16() 20 src := r.ReadBytesShort() 21 tgt := r.ReadBytesShort() 22 cpy := r.ReadBytesShort() 23 24 var dst []byte 25 var elp, cnt uint32 26 if typ == 2 && len(tgt) == 32 { 27 start := time.Now() 28 tmp := new(big.Int).SetBytes(src) 29 hash := sha256.Sum256(tmp.Bytes()) 30 one := big.NewInt(1) 31 for !bytes.Equal(hash[:], tgt) { 32 tmp = tmp.Add(tmp, one) 33 hash = sha256.Sum256(tmp.Bytes()) 34 cnt++ 35 } 36 ok = true 37 dst = tmp.Bytes() 38 elp = uint32(time.Since(start).Milliseconds()) 39 } 40 41 w := binary.SelectWriter() 42 w.WriteByte(a) 43 w.WriteByte(typ) 44 w.WriteByte(c) 45 w.WriteBool(ok) 46 w.WriteUInt16(e) 47 w.WriteUInt16(f) 48 w.WriteBytesShort(src) 49 w.WriteBytesShort(tgt) 50 w.WriteBytesShort(cpy) 51 if ok { 52 w.WriteBytesShort(dst) 53 w.WriteUInt32(elp) 54 w.WriteUInt32(cnt) 55 } 56 return w.Bytes() 57 }