将十六进制字符串哈希转换为 base64

发布于 2025-01-16 20:05:01 字数 897 浏览 3 评论 0原文

我有一个明文密码“welcome2022”。

预期的 SHA1 哈希为 wjC4KfO5XfMIRhi45M/VA/0i8NA=

但是,sql 的 SHA1 哈希方法正在生成十六进制字符串格式的 SHA1 哈希,即 C230B829F3B95DF3084618B8E4CFD503FD22F0D0

我打算将上面的十六进制字符串转换为 Base64 编码的哈希 wjC4KfO5XfMIRhi45M/VA/0i8NA=

下面是groovy代码供您参考。

    def cleartext = SecurityUtil.decrypt(password) //Decrypts the value of a GuardedString.    
   def password_bytes = [0, 0, 0, 0, 0] as byte[]
    password_bytes = SecurityUtil.charsToBytes(cleartext.toCharArray())
    def password2 = SecurityUtil.computeBase64SHA1Hash(password_bytes) //Computes the base 64 encoded SHA1 hash of the input.

然后运行下面的 sql 查询,

sql.eachRow("SELECT id FROM users WHERE userName = ? AND password =CONCAT('{SHA1}', ?)", [username, password2]) {
....
}

执行此 groovy 脚本时日志不会显示任何错误。

我不确定我编写的代码语法是否正确。

I have a plaintext password "welcome2022".

Expected SHA1 hash is wjC4KfO5XfMIRhi45M/VA/0i8NA=

However, SHA1 hash method of sql is generating SHA1 hash in hex string format which is C230B829F3B95DF3084618B8E4CFD503FD22F0D0.

I intend to convert the the above hex string to the base64 encoded hash wjC4KfO5XfMIRhi45M/VA/0i8NA=.

Below is the groovy code for your reference.

    def cleartext = SecurityUtil.decrypt(password) //Decrypts the value of a GuardedString.    
   def password_bytes = [0, 0, 0, 0, 0] as byte[]
    password_bytes = SecurityUtil.charsToBytes(cleartext.toCharArray())
    def password2 = SecurityUtil.computeBase64SHA1Hash(password_bytes) //Computes the base 64 encoded SHA1 hash of the input.

Then running the below sql query,

sql.eachRow("SELECT id FROM users WHERE userName = ? AND password =CONCAT('{SHA1}', ?)", [username, password2]) {
....
}

Logs do not show any errors when this groovy script is executed.

I am not sure if I am writing the code correct syntactically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2025-01-23 20:05:01

因此,此函数将为您提供给定密码的 base64 编码的 SHA-1 哈希值:

def sha64(String password) {
    password.digest('SHA-1').decodeHex().encodeBase64()
}

如果您这样做:

println sha64('welcome2022')

它会打印

wjC4KfO5XfMIRhi45M/VA/0i8NA=

So this function will give you the base64 encoded SHA-1 hash of a given password:

def sha64(String password) {
    password.digest('SHA-1').decodeHex().encodeBase64()
}

And if you do:

println sha64('welcome2022')

It prints

wjC4KfO5XfMIRhi45M/VA/0i8NA=
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文