encoding.go 2.5 KB

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