Replies: 1 comment 1 reply
-
根据文件 alist/internal/model/user.go 里面的代码: const StaticHashSalt = "https://github.com/alist-org/alist"
// ...
func (u *User) SetPassword(pwd string) *User {
u.Salt = random.String(16)
u.PwdHash = TwoHashPwd(pwd, u.Salt)
u.PwdTS = time.Now().Unix()
return u
}
func StaticHash(password string) string {
return utils.HashData(utils.SHA256, []byte(fmt.Sprintf("%s-%s", password, StaticHashSalt)))
}
func HashPwd(static string, salt string) string {
return utils.HashData(utils.SHA256, []byte(fmt.Sprintf("%s-%s", static, salt)))
}
func TwoHashPwd(password string, salt string) string {
return HashPwd(StaticHash(password), salt)
} Alist 在这里基本上就是拿明文密码,用 SHA256 Hash 两次,第一次 Hash 用的值是 接下来第二次 Hash 的结果就是拿来当 我用 Python 重新写了一遍 Alist 的密码生成逻辑: import hashlib
import string
import random
STATIC_HASH_SALT = "https://github.com/alist-org/alist"
def sha256(s: str) -> str:
hasher = hashlib.sha256()
hasher.update(s.encode("ASCII"))
return hasher.hexdigest()
def random_string(length: int) -> str:
rand_str = ""
for _ in range(length):
rand_str += random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits)
return rand_str
plaintext_pwd = input("Please enter password: ")
plaintext_pwd = plaintext_pwd.strip()
salt = input("Please manually enter salt (Just press [Enter] if you want random salt): ")
if not salt:
salt = random_string(16)
iter1_plaintext = plaintext_pwd + "-" + STATIC_HASH_SALT
print("Password + StaticSalt:", iter1_plaintext)
iter1_hash = sha256(iter1_plaintext)
print("[Hash] Password + StaticSalt = StaticSaltPasswordHash:", iter1_hash)
iter2_plaintext = iter1_hash + "-" + salt
print("StaticSaltPasswordHash + Salt:", iter2_plaintext)
iter2_hash = sha256(iter2_plaintext)
print("[Hash] StaticSaltPasswordHash + Salt = pwd_hash:", iter2_hash)
print("Result:", iter2_hash) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
我想知道在数据库中的用户密码pwd_hash是如何计算出来的,谢谢
Beta Was this translation helpful? Give feedback.
All reactions