github.com/godaddy-x/freego@v1.0.156/utils/bcrypt.go (about)

     1  package utils
     2  
     3  func newKey() string {
     4  	return MD5(AddStr(GetUUID(), AnyToStr(UnixNano()), RandStr(32)))
     5  }
     6  
     7  func newHash(password, salt, key string) string {
     8  	return HMAC_SHA512(HMAC_SHA512(password, key), HMAC_SHA512(HMAC_SHA512(salt, key), HMAC_SHA512(GetLocalSecretKey(), key)))
     9  }
    10  
    11  func newHashR(hash, salt, key string) string {
    12  	return HMAC_SHA256(HMAC_SHA512(hash, key), HMAC_SHA512(HMAC_SHA512(salt, key), HMAC_SHA512(GetLocalSecretKey(), key)))
    13  }
    14  
    15  func recHash(hash, salt, key string, index int) string {
    16  	if index > 16 {
    17  		return hash
    18  	}
    19  	hash = HMAC_SHA512(hash, HMAC_SHA256(salt, key))
    20  	index++
    21  	return recHash(hash, salt, key, index)
    22  }
    23  
    24  func mergeHash(hash, key string) string {
    25  	return AddStr(hash, key)
    26  }
    27  
    28  func PasswordHash(password, salt string) string {
    29  	key := newKey()
    30  	hash := newHash(password, salt, key)
    31  	hash = recHash(hash, salt, key, 0)
    32  	return mergeHash(newHashR(hash, salt, key), key)
    33  }
    34  
    35  func PasswordVerify(password, salt, target string) bool {
    36  	if len(target) != 96 {
    37  		return false
    38  	}
    39  	key := target[64:]
    40  	hash := newHash(password, salt, key)
    41  	hash = recHash(hash, salt, key, 0)
    42  	return mergeHash(newHashR(hash, salt, key), key) == target
    43  }