security.go 723 B

123456789101112131415161718192021222324252627282930313233343536
  1. package world
  2. import "math/rand"
  3. import "crypto/sha1"
  4. import "fmt"
  5. const MAKE_SALT_AID string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789."
  6. func MakeSalt() string {
  7. c1 := MAKE_SALT_AID[rand.Intn(len(MAKE_SALT_AID))]
  8. c2 := MAKE_SALT_AID[rand.Intn(len(MAKE_SALT_AID))]
  9. res := string( []byte{ c1, c2 })
  10. return res
  11. }
  12. func WoeCryptPassword(password string, salt string) string {
  13. if len(salt) < 1 {
  14. salt = MakeSalt()
  15. }
  16. to_hash := salt + password
  17. return salt + fmt.Sprintf("%x", sha1.Sum([]byte(to_hash)))
  18. }
  19. func WoeCryptChallenge(hash, trypass string) bool {
  20. salt := hash[0:2]
  21. try := WoeCryptPassword(trypass, salt)
  22. return try == hash
  23. }