util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package gopay
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "time"
  6. )
  7. // 获取随机字符串
  8. // length:字符串长度
  9. func GetRandomString(length int) string {
  10. str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
  11. var (
  12. result []byte
  13. b []byte
  14. r *rand.Rand
  15. )
  16. b = []byte(str)
  17. r = rand.New(rand.NewSource(time.Now().UnixNano()))
  18. for i := 0; i < length; i++ {
  19. result = append(result, b[r.Intn(len(b))])
  20. }
  21. return string(result)
  22. }
  23. // 解析时间
  24. func ParseDateTime(timeStr string) (datetime time.Time) {
  25. datetime, _ = time.ParseInLocation(TimeLayout, timeStr, time.Local)
  26. return
  27. }
  28. // 字符串转Float64
  29. func String2Float64(floatStr string) (floatNum float64) {
  30. floatNum, _ = strconv.ParseFloat(floatStr, 64)
  31. return
  32. }
  33. // 字符串转Float32
  34. func String2Float32(floatStr string) (floatNum float32) {
  35. float, _ := strconv.ParseFloat(floatStr, 32)
  36. floatNum = float32(float)
  37. return
  38. }
  39. // Float64转字符串
  40. // floatNum:float64数字
  41. // prec:精度位数(不传则默认float数字精度)
  42. func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
  43. if len(prec) > 0 {
  44. floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64)
  45. return
  46. }
  47. floatStr = strconv.FormatFloat(floatNum, 'f', -1, 64)
  48. return
  49. }
  50. // Float32转字符串
  51. // floatNum:float32数字
  52. // prec:精度位数(不传则默认float数字精度)
  53. func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
  54. if len(prec) > 0 {
  55. floatStr = strconv.FormatFloat(float64(floatNum), 'f', prec[0], 32)
  56. return
  57. }
  58. floatStr = strconv.FormatFloat(float64(floatNum), 'f', -1, 32)
  59. return
  60. }
  61. // 字符串转Int
  62. func String2Int(intStr string) (intNum int) {
  63. intNum, _ = strconv.Atoi(intStr)
  64. return
  65. }
  66. // 字符串转Int32
  67. func String2Int32(intStr string) (int32Num int32) {
  68. intNum, _ := strconv.Atoi(intStr)
  69. int32Num = int32(intNum)
  70. return
  71. }
  72. // 字符串转Int64
  73. func String2Int64(intStr string) (int64Num int64) {
  74. intNum, _ := strconv.Atoi(intStr)
  75. int64Num = int64(intNum)
  76. return
  77. }
  78. // Int转字符串
  79. func Int2String(intNum int) (intStr string) {
  80. intStr = strconv.Itoa(intNum)
  81. return
  82. }
  83. // Int32转字符串
  84. func Int322String(intNum int32) (int32Str string) {
  85. // 10, 代表10进制
  86. int32Str = strconv.FormatInt(int64(intNum), 10)
  87. return
  88. }
  89. // Int64转字符串
  90. func Int642String(intNum int64) (int64Str string) {
  91. // 10, 代表10进制
  92. int64Str = strconv.FormatInt(intNum, 10)
  93. return
  94. }
  95. // 解密填充模式(去除补全码) PKCS7UnPadding
  96. // 解密时,需要在最后面去掉加密时添加的填充byte
  97. func PKCS7UnPadding(origData []byte) (bs []byte) {
  98. length := len(origData)
  99. unPaddingNumber := int(origData[length-1]) // 找到Byte数组最后的填充byte 数字
  100. if unPaddingNumber <= 16 {
  101. bs = origData[:(length - unPaddingNumber)] // 只截取返回有效数字内的byte数组
  102. } else {
  103. bs = origData
  104. }
  105. return
  106. }
  107. // 解密填充模式(去除补全码) PKCS5UnPadding
  108. // 解密时,需要在最后面去掉加密时添加的填充byte
  109. func PKCS5UnPadding(origData []byte) (bs []byte) {
  110. length := len(origData)
  111. unPaddingNumber := int(origData[length-1]) // 找到Byte数组最后的填充byte
  112. if unPaddingNumber <= 16 {
  113. bs = origData[:(length - unPaddingNumber)] // 只截取返回有效数字内的byte数组
  114. } else {
  115. bs = origData
  116. }
  117. return
  118. }