github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/jls/fake_random.go (about) 1 // DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 // Version 2, December 2004 3 4 // Copyright 2023 Jimmy Huang 5 6 // Everyone is permitted to copy and distribute verbatim or modified 7 // copies of this license document, and changing it is allowed as long 8 // as the name is changed. 9 10 // DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 // TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 13 // 0. You just DO WHAT THE FUCK YOU WANT TO. 14 15 package jls 16 17 import ( 18 "crypto/rand" 19 "crypto/sha256" 20 "io" 21 ) 22 23 type FakeRandom struct { 24 N []byte 25 Random []byte 26 27 PWD []byte 28 IV []byte 29 } 30 31 func NewFakeRandom(PWD []byte, IV []byte) *FakeRandom { 32 pwd := sha256.New() 33 pwd.Write(PWD) 34 iv := sha256.New() 35 iv.Write(IV) 36 37 return &FakeRandom{PWD: pwd.Sum(nil), IV: iv.Sum(nil)} 38 } 39 40 func (f *FakeRandom) Build() error { 41 n := make([]byte, 16) 42 random := make([]byte, 32) 43 io.ReadFull(rand.Reader, n) 44 45 random, err := Encrypt(f.IV, n, f.PWD) 46 if err != nil { 47 return err 48 } 49 f.Random = random 50 f.N = n 51 return nil 52 } 53 54 func (f *FakeRandom) Check(random []byte) (bool, error) { 55 n, err := Decrypt(f.IV, random, f.PWD) 56 if err != nil { 57 return false, err 58 } 59 f.Random = random 60 f.N = n 61 return true, nil 62 }