简体   繁体   English

如何解析 Spring Security 散列的密码哈希?

[英]How to parse the password hash hashed by Spring Security?

I want to migrate my users from my application to an external SSO.我想将我的用户从我的应用程序迁移到外部 SSO。 I want my users to keep their existing passwords, so I'd like to retrieve the hashed password and provide it to the SSO application.我希望我的用户保留他们现有的密码,因此我想检索散列密码并将其提供给 SSO 应用程序。 The algorithm is the same on both sides (pbkdf2-sha256) so it's supposed to work.两边的算法(pbkdf2-sha256)都是一样的,所以它应该可以工作。

Thus I need to be able to retrieve the salt and hashed password from the entry in the database.因此,我需要能够从数据库中的条目中检索盐和散列密码。

My application uses Spring Security.我的应用程序使用 Spring Security。

It seems that I need to do this manually, as the interface PasswordEncoder, which Pbkdf2PasswordEncoder implements, does not provide a way to retrieve the initial elements from the stored value.似乎我需要手动执行此操作,因为 Pbkdf2PasswordEncoder 实现的接口 PasswordEncoder 没有提供从存储值中检索初始元素的方法。

What would be the correct way to achieve this ?实现这一目标的正确方法是什么?

Thanks谢谢

edit:编辑:

Here is my code这是我的代码

Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder("", 27500, 512);
String encoded = encoder.encode("password");
System.out.println(encoded); // prints the hex encoded hashed password: 2a319aaf0252d77e671cf1b074f149f7eed1b362afb47bef84e9b01a8140b26a733cd22df007d68668915c7bd51af9eefda1662216a184fa7eb034c176ce9518fc83f3fd935a2d3d
final byte[] digested = Hex.decode(encoded);
byte[] salt = EncodingUtils.subArray(digested, 0, 7);
byte[] password = EncodingUtils.subArray(digested, 8, digested.length);
System.out.println(new String(Base64.getEncoder().encode(salt))); // prints KjGarwJS134=
System.out.println(new String(Base64.getEncoder().encode(password))); // prints HPGwdPFJ9+7Rs2KvtHvvhOmwGoFAsmpzPNIt8AfWhmiRXHvVGvnu/aFmIhahhPp+sDTBds6VGPyD8/2TWi09

If I then go to this website and enter the following values:如果我然后转到此网站并输入以下值:

  • master password: password主密码:密码
  • salt: KjGarwJS134=盐:KjGarwJS134=
  • iteration: 27500迭代次数:27500
  • dkLen: 512 dkLen:512
  • PBE Ciphers: sha256 PBE 密码:sha256

Then I would expect the output to match the last output of my code, but this isn't the case.然后我希望输出与我的代码的最后一个输出相匹配,但事实并非如此。

You obviously cannot get the password back from the hash, but I don't think you're asking that.您显然无法从哈希中取回密码,但我认为您不是在问那个。 Here's how you would do it:以下是您的操作方法:

  1. First check if it is base64.首先检查它是否是base64。 If so, take your string and base64-decode it, turning it into a byte array.如果是这样,请获取您的字符串并对其进行 base64 解码,将其转换为字节数组。 (The default is just hex nibbles, ie option 2). (默认值只是十六进制半字节,即选项 2)。
  2. If it wasn't, then it's just hex nibbles.如果不是,那么它只是十六进制半字节。 You can tell;你可以说; if the entire thing consists solely of digits and the letters af, it's this, otherwise it's #1 (base64).如果整个事物仅由数字和字母 af 组成,则为这个,否则为 #1 (base64)。 Parsing hex nibbles back to a byte array is fairly trivial;将十六进制半字节解析回字节数组相当简单; toss every 2 characters through parseInt: Integer.parseInt(in.substring(x, x + 2), 0x10);通过 parseInt 每 2 个字符折腾: Integer.parseInt(in.substring(x, x + 2), 0x10); - the 0x10 at the end is important. - 最后的 0x10 很重要。
  3. The byte array obtained with this is the salt and the hash, concatenating, in that order.由此获得的字节数组是按顺序连接的盐和散列。 By default, the salt would be 8 bytes, but this is all configurable in spring.默认情况下,salt 为 8 个字节,但这在 spring 中都是可配置的。 So, lop off the first 8 bytes from the array - that's your salt.因此,从数组中删除前 8 个字节 - 这就是您的盐。
  4. The rest is 'just' the raw hash.其余的是“只是”原始哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Security 3哈希密码编码器 - Spring Security 3 Hash Password Encoders Spring安全密码hash + salt - Spring security password hash + salt Spring Security-使用BCrypt哈希密码的用户身份验证(错误的凭证错误) - Spring Security - user authentication with BCrypt hashed password (bad credentials error) 带有哈希密码的Spring TokenBasedRememberMeServices - Spring TokenBasedRememberMeServices with hashed password 如何将文本密码与使用 spring 数据 jpa 散列的密码进行比较? - How to compare text password to password that is hashed using spring data jpa? Spring Boot,MySQL:如何使用哈希密码进行数据库身份验证? - Spring Boot, MySQL: How to do a database authentication with a hashed password? 使用Java中的SHA-256算法比较使用相同盐的两个哈希密码,Spring安全吗? - Compare two hashed password using same salt using SHA-256 algorith in Java, Spring security? 为什么我可以使用Java Spring Security中的密码哈希进行身份验证 - Why am i able to authenticate with the password hash in Java Spring Security 使用Java验证在PHP中使用password_hash散列的密码 - Using Java to Verify a Password Hashed using password_hash in PHP 如何解密pbkdf的散列密码 - How to decrypt the hashed password of pbkdf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM