encoding.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package util
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "errors"
  10. "fmt"
  11. "hash"
  12. "strings"
  13. )
  14. // source: https://github.com/gogits/gogs/blob/9ee80e3e5426821f03a4e99fad34418f5c736413/modules/base/tool.go#L58
  15. func GetRandomString(n int, alphabets ...byte) string {
  16. const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  17. var bytes = make([]byte, n)
  18. rand.Read(bytes)
  19. for i, b := range bytes {
  20. if len(alphabets) == 0 {
  21. bytes[i] = alphanum[b%byte(len(alphanum))]
  22. } else {
  23. bytes[i] = alphabets[b%byte(len(alphabets))]
  24. }
  25. }
  26. return string(bytes)
  27. }
  28. func EncodePassword(password string, salt string) string {
  29. newPasswd := PBKDF2([]byte(password), []byte(salt), 10000, 50, sha256.New)
  30. return fmt.Sprintf("%x", newPasswd)
  31. }
  32. // Encode string to md5 hex value.
  33. func EncodeMd5(str string) string {
  34. m := md5.New()
  35. m.Write([]byte(str))
  36. return hex.EncodeToString(m.Sum(nil))
  37. }
  38. // http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto
  39. func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
  40. prf := hmac.New(h, password)
  41. hashLen := prf.Size()
  42. numBlocks := (keyLen + hashLen - 1) / hashLen
  43. var buf [4]byte
  44. dk := make([]byte, 0, numBlocks*hashLen)
  45. U := make([]byte, hashLen)
  46. for block := 1; block <= numBlocks; block++ {
  47. // N.B.: || means concatenation, ^ means XOR
  48. // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
  49. // U_1 = PRF(password, salt || uint(i))
  50. prf.Reset()
  51. prf.Write(salt)
  52. buf[0] = byte(block >> 24)
  53. buf[1] = byte(block >> 16)
  54. buf[2] = byte(block >> 8)
  55. buf[3] = byte(block)
  56. prf.Write(buf[:4])
  57. dk = prf.Sum(dk)
  58. T := dk[len(dk)-hashLen:]
  59. copy(U, T)
  60. // U_n = PRF(password, U_(n-1))
  61. for n := 2; n <= iter; n++ {
  62. prf.Reset()
  63. prf.Write(U)
  64. U = U[:0]
  65. U = prf.Sum(U)
  66. for x := range U {
  67. T[x] ^= U[x]
  68. }
  69. }
  70. }
  71. return dk[:keyLen]
  72. }
  73. func GetBasicAuthHeader(user string, password string) string {
  74. var userAndPass = user + ":" + password
  75. return "Basic " + base64.StdEncoding.EncodeToString([]byte(userAndPass))
  76. }
  77. func DecodeBasicAuthHeader(header string) (string, string, error) {
  78. var code string
  79. parts := strings.SplitN(header, " ", 2)
  80. if len(parts) == 2 && parts[0] == "Basic" {
  81. code = parts[1]
  82. }
  83. decoded, err := base64.StdEncoding.DecodeString(code)
  84. if err != nil {
  85. return "", "", err
  86. }
  87. userAndPass := strings.SplitN(string(decoded), ":", 2)
  88. if len(userAndPass) != 2 {
  89. return "", "", errors.New("Invalid basic auth header")
  90. }
  91. return userAndPass[0], userAndPass[1], nil
  92. }