将十六进制字符串哈希转换为 base64
我有一个明文密码“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,此函数将为您提供给定密码的 base64 编码的 SHA-1 哈希值:
如果您这样做:
它会打印
So this function will give you the base64 encoded SHA-1 hash of a given password:
And if you do:
It prints