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

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

本文整理了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

/**
 * Hash a password using the OpenBSD bcrypt scheme
 * @param password    the password to hash
 * @param salt    the salt to hash with (perhaps generated
 * using BCrypt.gensalt)
 * @return    the hashed password
 */
public static String hashpw(String password, String salt) {
  byte passwordb[];
  try {
    passwordb = password.getBytes("UTF-8");
  } catch (UnsupportedEncodingException uee) {
    throw new AssertionError("UTF-8 is not supported");
  }
  return hashpw(passwordb, salt);
}

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

/**
 * Check that a plaintext password matches a previously hashed
 * one
 * @param plaintext    the plaintext password to verify
 * @param hashed    the previously-hashed password
 * @return    true if the passwords match, false otherwise
 */
public static boolean checkpw(String plaintext, String hashed) {
  return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
}

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

/**
 * Check that a plaintext password matches a previously hashed one
 * @param plaintext the plaintext password to verify
 * @param hashed the previously-hashed password
 * @return true if the passwords match, false otherwise
 */
public static boolean checkpw(String plaintext, String hashed) {
  return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
}

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

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

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

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

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

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

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

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

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

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

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

public String encode(CharSequence rawPassword) {
  String salt;
  if (strength > 0) {
    if (random != null) {
      salt = BCrypt.gensalt(strength, random);
    }
    else {
      salt = BCrypt.gensalt(strength);
    }
  }
  else {
    salt = BCrypt.gensalt();
  }
  return BCrypt.hashpw(rawPassword.toString(), salt);
}

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

public String encode(CharSequence rawPassword) {
  String salt;
  if (strength > 0) {
    if (random != null) {
      salt = BCrypt.gensalt(version.getVersion(), strength, random);
    } else {
      salt = BCrypt.gensalt(version.getVersion(), strength);
    }
  } else {
    salt = BCrypt.gensalt(version.getVersion());
  }
  return BCrypt.hashpw(rawPassword.toString(), salt);
}

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

/**
 * Test method for 'BCrypt.gensalt()'
 */
@Test
public void testGensalt() {
  print("BCrypt.gensalt(): ");
  for (int i = 0; i < test_vectors.length; i += 4) {
    String plain = test_vectors[i][0];
    String salt = BCrypt.gensalt();
    String hashed1 = BCrypt.hashpw(plain, salt);
    String hashed2 = BCrypt.hashpw(plain, hashed1);
    assertThat(hashed2).isEqualTo(hashed1);
    print(".");
  }
  println("");
}

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

/**
 * Test method for 'BCrypt.gensalt(int)'
 */
@Test
public void testGensaltInt() {
  print("BCrypt.gensalt(log_rounds):");
  for (int i = 4; i <= 12; i++) {
    print(" " + Integer.toString(i) + ":");
    for (int j = 0; j < test_vectors.length; j += 4) {
      String plain = test_vectors[j][0];
      String salt = BCrypt.gensalt(i);
      String hashed1 = BCrypt.hashpw(plain, salt);
      String hashed2 = BCrypt.hashpw(plain, hashed1);
      assertThat(hashed2).isEqualTo(hashed1);
      print(".");
    }
  }
  println("");
}

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

/**
 * Test method for 'BCrypt.hashpw(String, String)'
 */
@Test
public void testHashpw() {
  print("BCrypt.hashpw(): ");
  for (int i = 0; i < test_vectors.length; i++) {
    String plain = test_vectors[i][0];
    String salt = test_vectors[i][1];
    String expected = test_vectors[i][2];
    String hashed = BCrypt.hashpw(plain, salt);
    assertThat(expected).isEqualTo(hashed);
    print(".");
  }
  println("");
}

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

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

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

/**
 * Test for correct hashing of non-US-ASCII passwords
 */
@Test
public void testInternationalChars() {
  print("BCrypt.hashpw w/ international chars: ");
  String pw1 = "ππππππππ";
  String pw2 = "????????";
  String h1 = BCrypt.hashpw(pw1, BCrypt.gensalt());
  assertThat(BCrypt.checkpw(pw2, h1)).isFalse();
  print(".");
  String h2 = BCrypt.hashpw(pw2, BCrypt.gensalt());
  assertThat(BCrypt.checkpw(pw1, h2)).isFalse();
  print(".");
  println("");
}

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

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

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

@Override
 public void run() {
  out.print(BCrypt.hashpw(password, BCrypt.gensalt()));
 }
}

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

/**
 * Check that a plaintext password matches a previously hashed one
 * @param plaintext the plaintext password to verify
 * @param hashed the previously-hashed password
 * @return true if the passwords match, false otherwise
 */
public static boolean checkpw(String plaintext, String hashed) {
  return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
}

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

/**
 * Check that a plaintext password matches a previously hashed
 * one
 * @param plaintext the plaintext password to verify
 * @param hashed    the previously-hashed password
 * @return  true if the passwords match, false otherwise
 */
public static boolean checkpw(String plaintext, String hashed) {
  return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
}

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

public String encode(CharSequence rawPassword) {
  String salt;
  if (strength > 0) {
    if (random != null) {
      salt = BCrypt.gensalt(strength, random);
    }
    else {
      salt = BCrypt.gensalt(strength);
    }
  }
  else {
    salt = BCrypt.gensalt();
  }
  return BCrypt.hashpw(rawPassword.toString(), salt);
}

相关文章