1
0

util.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "crypto/md5"
  17. "crypto/rand"
  18. "encoding/hex"
  19. "fmt"
  20. )
  21. // RandId return a rand string used in frp.
  22. func RandId() (id string, err error) {
  23. return RandIdWithLen(8)
  24. }
  25. // RandIdWithLen return a rand string with idLen length.
  26. func RandIdWithLen(idLen int) (id string, err error) {
  27. b := make([]byte, idLen)
  28. _, err = rand.Read(b)
  29. if err != nil {
  30. return
  31. }
  32. id = fmt.Sprintf("%x", b)
  33. return
  34. }
  35. func GetAuthKey(token string, timestamp int64) (key string) {
  36. token = token + fmt.Sprintf("%d", timestamp)
  37. md5Ctx := md5.New()
  38. md5Ctx.Write([]byte(token))
  39. data := md5Ctx.Sum(nil)
  40. return hex.EncodeToString(data)
  41. }