org.springframework.security.crypto.bcrypt.BCrypt.hashpw()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(254)

本文整理了Java中org.springframework.security.crypto.bcrypt.BCrypt.hashpw()方法的一些代码示例,展示了BCrypt.hashpw()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BCrypt.hashpw()方法的具体详情如下:
包路径:org.springframework.security.crypto.bcrypt.BCrypt
类名称:BCrypt
方法名:hashpw

BCrypt.hashpw介绍

[英]Hash a password using the OpenBSD bcrypt scheme
[中]使用OpenBSD bcrypt方案散列密码

代码示例

代码示例来源:origin: spring-projects/spring-security

  1. /**
  2. * Hash a password using the OpenBSD bcrypt scheme
  3. * @param password the password to hash
  4. * @param salt the salt to hash with (perhaps generated
  5. * using BCrypt.gensalt)
  6. * @return the hashed password
  7. */
  8. public static String hashpw(String password, String salt) {
  9. byte passwordb[];
  10. try {
  11. passwordb = password.getBytes("UTF-8");
  12. } catch (UnsupportedEncodingException uee) {
  13. throw new AssertionError("UTF-8 is not supported");
  14. }
  15. return hashpw(passwordb, salt);
  16. }

代码示例来源:origin: spring-projects/spring-security

  1. /**
  2. * Check that a plaintext password matches a previously hashed
  3. * one
  4. * @param plaintext the plaintext password to verify
  5. * @param hashed the previously-hashed password
  6. * @return true if the passwords match, false otherwise
  7. */
  8. public static boolean checkpw(String plaintext, String hashed) {
  9. return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
  10. }

代码示例来源:origin: org.springframework.security/spring-security-core

  1. /**
  2. * Check that a plaintext password matches a previously hashed one
  3. * @param plaintext the plaintext password to verify
  4. * @param hashed the previously-hashed password
  5. * @return true if the passwords match, false otherwise
  6. */
  7. public static boolean checkpw(String plaintext, String hashed) {
  8. return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
  9. }

代码示例来源:origin: spring-projects/spring-security

  1. @Test
  2. public void hashpwWorksWithOldRevision() {
  3. assertThat(BCrypt.hashpw("password", "$2$05$......................")).isEqualTo(
  4. "$2$05$......................bvpG2UfzdyW/S0ny/4YyEZrmczoJfVm");
  5. }

代码示例来源:origin: spring-projects/spring-security

  1. @Test(expected = IllegalArgumentException.class)
  2. public void hashpwFailsWhenSaltSpecifiesTooFewRounds() {
  3. BCrypt.hashpw("password", "$2a$03$......................");
  4. }

代码示例来源:origin: spring-projects/spring-security

  1. @Test(expected = IllegalArgumentException.class)
  2. public void saltLengthIsChecked() {
  3. BCrypt.hashpw("", "");
  4. }

代码示例来源:origin: spring-projects/spring-security

  1. @Test(expected = IllegalArgumentException.class)
  2. public void hashpwFailsWhenSaltIsNull() {
  3. BCrypt.hashpw("password", null);
  4. }

代码示例来源:origin: spring-projects/spring-security

  1. @Test(expected = IllegalArgumentException.class)
  2. public void hashpwFailsWhenSaltSpecifiesTooManyRounds() {
  3. BCrypt.hashpw("password", "$2a$32$......................");
  4. }

代码示例来源:origin: org.springframework.security/spring-security-core

  1. public String encode(CharSequence rawPassword) {
  2. String salt;
  3. if (strength > 0) {
  4. if (random != null) {
  5. salt = BCrypt.gensalt(strength, random);
  6. }
  7. else {
  8. salt = BCrypt.gensalt(strength);
  9. }
  10. }
  11. else {
  12. salt = BCrypt.gensalt();
  13. }
  14. return BCrypt.hashpw(rawPassword.toString(), salt);
  15. }

代码示例来源:origin: spring-projects/spring-security

  1. public String encode(CharSequence rawPassword) {
  2. String salt;
  3. if (strength > 0) {
  4. if (random != null) {
  5. salt = BCrypt.gensalt(version.getVersion(), strength, random);
  6. } else {
  7. salt = BCrypt.gensalt(version.getVersion(), strength);
  8. }
  9. } else {
  10. salt = BCrypt.gensalt(version.getVersion());
  11. }
  12. return BCrypt.hashpw(rawPassword.toString(), salt);
  13. }

代码示例来源:origin: spring-projects/spring-security

  1. /**
  2. * Test method for 'BCrypt.gensalt()'
  3. */
  4. @Test
  5. public void testGensalt() {
  6. print("BCrypt.gensalt(): ");
  7. for (int i = 0; i < test_vectors.length; i += 4) {
  8. String plain = test_vectors[i][0];
  9. String salt = BCrypt.gensalt();
  10. String hashed1 = BCrypt.hashpw(plain, salt);
  11. String hashed2 = BCrypt.hashpw(plain, hashed1);
  12. assertThat(hashed2).isEqualTo(hashed1);
  13. print(".");
  14. }
  15. println("");
  16. }

代码示例来源:origin: spring-projects/spring-security

  1. /**
  2. * Test method for 'BCrypt.gensalt(int)'
  3. */
  4. @Test
  5. public void testGensaltInt() {
  6. print("BCrypt.gensalt(log_rounds):");
  7. for (int i = 4; i <= 12; i++) {
  8. print(" " + Integer.toString(i) + ":");
  9. for (int j = 0; j < test_vectors.length; j += 4) {
  10. String plain = test_vectors[j][0];
  11. String salt = BCrypt.gensalt(i);
  12. String hashed1 = BCrypt.hashpw(plain, salt);
  13. String hashed2 = BCrypt.hashpw(plain, hashed1);
  14. assertThat(hashed2).isEqualTo(hashed1);
  15. print(".");
  16. }
  17. }
  18. println("");
  19. }

代码示例来源:origin: spring-projects/spring-security

  1. /**
  2. * Test method for 'BCrypt.hashpw(String, String)'
  3. */
  4. @Test
  5. public void testHashpw() {
  6. print("BCrypt.hashpw(): ");
  7. for (int i = 0; i < test_vectors.length; i++) {
  8. String plain = test_vectors[i][0];
  9. String salt = test_vectors[i][1];
  10. String expected = test_vectors[i][2];
  11. String hashed = BCrypt.hashpw(plain, salt);
  12. assertThat(expected).isEqualTo(hashed);
  13. print(".");
  14. }
  15. println("");
  16. }

代码示例来源:origin: cloudfoundry/uaa

  1. @Test
  2. public void testSameSaltHash() {
  3. String salt = BCrypt.gensalt();
  4. String passwd = "testpassword"+new RandomValueStringGenerator().generate();
  5. Assert.assertEquals(BCrypt.hashpw(passwd, salt), BCrypt.hashpw(passwd, salt));
  6. }

代码示例来源:origin: spring-projects/spring-security

  1. /**
  2. * Test for correct hashing of non-US-ASCII passwords
  3. */
  4. @Test
  5. public void testInternationalChars() {
  6. print("BCrypt.hashpw w/ international chars: ");
  7. String pw1 = "ππππππππ";
  8. String pw2 = "????????";
  9. String h1 = BCrypt.hashpw(pw1, BCrypt.gensalt());
  10. assertThat(BCrypt.checkpw(pw2, h1)).isFalse();
  11. print(".");
  12. String h2 = BCrypt.hashpw(pw2, BCrypt.gensalt());
  13. assertThat(BCrypt.checkpw(pw1, h2)).isFalse();
  14. print(".");
  15. println("");
  16. }

代码示例来源:origin: com.erudika/para

  1. /**
  2. * bcrypt hash function implemented by Spring Security
  3. * @param s the string to be hashed
  4. * @return the hash
  5. */
  6. public static String bcrypt(String s) {
  7. return (s == null) ? s : BCrypt.hashpw(s, BCrypt.gensalt(12));
  8. }

代码示例来源:origin: PegaSysEng/pantheon

  1. @Override
  2. public void run() {
  3. out.print(BCrypt.hashpw(password, BCrypt.gensalt()));
  4. }
  5. }

代码示例来源:origin: org.springframework.security/spring-security-crypto

  1. /**
  2. * Check that a plaintext password matches a previously hashed one
  3. * @param plaintext the plaintext password to verify
  4. * @param hashed the previously-hashed password
  5. * @return true if the passwords match, false otherwise
  6. */
  7. public static boolean checkpw(String plaintext, String hashed) {
  8. return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
  9. }

代码示例来源:origin: org.springframework.security/org.springframework.security.core

  1. /**
  2. * Check that a plaintext password matches a previously hashed
  3. * one
  4. * @param plaintext the plaintext password to verify
  5. * @param hashed the previously-hashed password
  6. * @return true if the passwords match, false otherwise
  7. */
  8. public static boolean checkpw(String plaintext, String hashed) {
  9. return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
  10. }

代码示例来源:origin: org.springframework.security/spring-security-crypto

  1. public String encode(CharSequence rawPassword) {
  2. String salt;
  3. if (strength > 0) {
  4. if (random != null) {
  5. salt = BCrypt.gensalt(strength, random);
  6. }
  7. else {
  8. salt = BCrypt.gensalt(strength);
  9. }
  10. }
  11. else {
  12. salt = BCrypt.gensalt();
  13. }
  14. return BCrypt.hashpw(rawPassword.toString(), salt);
  15. }

相关文章