seclib.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2019 Yunion
  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 seclib
  15. import (
  16. "bytes"
  17. "fmt"
  18. "math/rand"
  19. "strings"
  20. "github.com/tredoe/osutil/user/crypt/sha512_crypt"
  21. "yunion.io/x/pkg/utils"
  22. )
  23. const (
  24. DIGITS = "23456789"
  25. LETTERS = "abcdefghjkmnpqrstuvwxyz"
  26. UPPERS = "ABCDEFGHJKMNPRSTUVWXYZ"
  27. PUNC = "@^-+="
  28. ALL_DIGITS = "0123456789"
  29. ALL_LETTERS = "abcdefghijklmnopqrstuvwxyz"
  30. ALL_UPPERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  31. ALL_PUNC = "~`!@#$%^&*()-_=+[]{}|:';\",./<>?"
  32. )
  33. var FIRSTCHARS = fmt.Sprintf("%s%s", LETTERS, UPPERS)
  34. var CHARS = fmt.Sprintf("%s%s%s", DIGITS, LETTERS, UPPERS)
  35. func RandomPassword(width int) string {
  36. if width < 6 {
  37. width = 6
  38. }
  39. for {
  40. var buf bytes.Buffer
  41. digitsCnt := 0
  42. letterCnt := 0
  43. upperCnt := 0
  44. for i := 0; i < width; i += 1 {
  45. var ch byte
  46. {
  47. var candidates string
  48. if i == 0 {
  49. candidates = FIRSTCHARS
  50. } else {
  51. candidates = CHARS
  52. }
  53. index := rand.Intn(len(candidates))
  54. ch = candidates[index]
  55. }
  56. if strings.IndexByte(DIGITS, ch) >= 0 {
  57. digitsCnt += 1
  58. } else if strings.IndexByte(LETTERS, ch) >= 0 {
  59. letterCnt += 1
  60. } else if strings.IndexByte(LETTERS, ch+32) >= 0 {
  61. upperCnt += 1
  62. }
  63. buf.WriteByte(ch)
  64. }
  65. if digitsCnt > 1 && letterCnt > 1 && upperCnt > 1 {
  66. return buf.String()
  67. }
  68. }
  69. return ""
  70. }
  71. func GeneratePassword(passwd string) (string, error) {
  72. salt := RandomPassword(8)
  73. sha512Crypt := sha512_crypt.New()
  74. return sha512Crypt.Generate([]byte(passwd), []byte(fmt.Sprintf("$6$%s", salt)))
  75. }
  76. var WEAK_PASSWORDS []string = []string{
  77. "Huawei@", "huawei@", "Admin@", "admin@", "Root@", "root@", "ABC@", "abc@", "ABCD@", "abcd@", "Huawei123@", "huawei123@", "Admin123@", "admin123@", "Root123@", "root123@", "Huawei#", "huawei#", "Admin#", "admin#", "Root#", "root#", "ABC#", "abc#", "ABCD#", "abcd#", "Huawei123#", "huawei123#", "Admin123#", "admin123#", "Root123#", "root123#,Huawei!", "huawei!", "Admin!", "admin!", "Root!", "root!", "ABC!", "abc!", "ABCD!", "abcd!", "Huawei123!", "huawei123!", "Admin123!", "admin123!", "Root123!", "root123!", "ABC123!", "abc123!", "Huawei@123", "huawei@123", "Admin@123", "admin@123", "Root@123", "root@123", "ABC@123", "abc@123", "123@Huawei", "123@Root", "123@abc", "Huawei123", "huawei123", "Admin123", "admin123", "Root123", "root123", "abc123", "Huawei_123", "huawei_123", "Admin_123", "admin_123", "Root_123", "root_123", "ABC_123", "abc_123", "123abc", "123abcd", "1234abc", "1234abcd", "abcd123", "abc1234", "abcd1234", "abcd@1234", "abcd1234!", "abcd_1234", "a123456", "123.com", "123@com", "123_com", "Huawei!@#", "huawei!@#", "Admin!@#", "admin!@#", "Root!@#", "root!@#", "Huawei!@", "huawei!@", "Admin!@", "admin!@", "Root!@", "root!@", "Huaweiroot", "HuaweiRoot", "huaweiroot", "huaweiRoot", "Huaweiadmin", "HuaweiAdmin", "huaweiadmin", "huaweiAdmin", "Adminroot", "AdminRoot", "adminRoot", "adminroot", "Rootadmin", "RootAdmin", "rootAdmin", "rootadmin", "Rootroot", "RootRoot", "rootroot", "Administrator", "Password", "Password123", "Password@123", "Password_123", "Password123!", "DDM@123", "ddM@123", "dDm@123",
  78. }
  79. var CHARS2 = fmt.Sprintf("%s%s%s%s", DIGITS, LETTERS, UPPERS, PUNC)
  80. func RandomPassword2(width int) string {
  81. for {
  82. password := randomPassword2(width)
  83. if !utils.IsInStringArray(password, WEAK_PASSWORDS) {
  84. return password
  85. }
  86. }
  87. }
  88. type PasswordStrength struct {
  89. Digits int
  90. Lowercases int
  91. Uppercases int
  92. Punctuats int
  93. Invalid []byte
  94. }
  95. func randomPassword2(width int) string {
  96. if width < 6 {
  97. width = 6
  98. }
  99. for {
  100. ps := PasswordStrength{}
  101. var buf bytes.Buffer
  102. for i := 0; i < width; i += 1 {
  103. var ch byte
  104. {
  105. var candidates string
  106. if i == 0 {
  107. candidates = FIRSTCHARS
  108. } else {
  109. candidates = CHARS2
  110. }
  111. index := rand.Intn(len(candidates))
  112. ch = CHARS2[index]
  113. }
  114. if strings.IndexByte(DIGITS, ch) >= 0 {
  115. ps.Digits += 1
  116. } else if strings.IndexByte(LETTERS, ch) >= 0 {
  117. ps.Lowercases += 1
  118. } else if strings.IndexByte(UPPERS, ch) >= 0 {
  119. ps.Uppercases += 1
  120. } else if strings.IndexByte(PUNC, ch) >= 0 {
  121. ps.Punctuats += 1
  122. }
  123. buf.WriteByte(ch)
  124. }
  125. if ps.Digits > 1 && ps.Lowercases > 1 && ps.Uppercases > 1 && ps.Punctuats >= 1 && ps.Punctuats <= 2 {
  126. return buf.String()
  127. }
  128. }
  129. }
  130. func AnalyzePasswordStrenth(passwd string) PasswordStrength {
  131. ps := PasswordStrength{Invalid: []byte{}}
  132. for i := 0; i < len(passwd); i += 1 {
  133. if strings.IndexByte(ALL_DIGITS, passwd[i]) >= 0 {
  134. ps.Digits += 1
  135. } else if strings.IndexByte(ALL_LETTERS, passwd[i]) >= 0 {
  136. ps.Lowercases += 1
  137. } else if strings.IndexByte(ALL_UPPERS, passwd[i]) >= 0 {
  138. ps.Uppercases += 1
  139. } else if strings.IndexByte(ALL_PUNC, passwd[i]) >= 0 {
  140. ps.Punctuats += 1
  141. } else {
  142. ps.Invalid = append(ps.Invalid, passwd[i])
  143. }
  144. }
  145. return ps
  146. }
  147. func (ps PasswordStrength) Len() int {
  148. return ps.Punctuats + ps.Uppercases + ps.Lowercases + ps.Digits
  149. }
  150. func (ps PasswordStrength) MeetComplexity() bool {
  151. if ps.Punctuats > 0 && ps.Digits > 0 && ps.Lowercases > 0 && ps.Uppercases > 0 && ps.Len() >= 12 {
  152. return true
  153. } else {
  154. return false
  155. }
  156. }
  157. func MeetComplxity(passwd string) bool {
  158. if utils.IsInStringArray(passwd, WEAK_PASSWORDS) {
  159. return false
  160. }
  161. ps := AnalyzePasswordStrenth(passwd)
  162. return ps.MeetComplexity()
  163. }