我收到错误消息“类型不兼容。必需:字节[]。找到:java.lang.string
我尝试了incompatible types found and required are same中的解决方案,该解决方案指出我必须初始化类型。我初始化了byte [],但仍然收到该错误
public static byte[] hash(char[] password, byte[] salt) {
PBEKeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
Arrays.fill(password, Character.MIN_VALUE);
try {
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte [] hashedPass = skf.generateSecret(spec).getEncoded();
return toHex(hashedPass);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new AssertionError("Error while hashing a password: " + e.getMessage(), e);
} finally {
spec.clearPassword();
}
}
public static String toHex(byte[] Array){
BigInteger bi = new BigInteger(1, Array);
String hex = bi.toString(16);
int paddingLength = (Array.length *2) - hex.length();
if (paddingLength > 0){
return String.format("%0" + paddingLength +"d", 0) + hex;
} else {
return hex;
}
}
我在第7行出现错误:
return toHex(hashedPass);
答案 0 :(得分:1)
方法hash(char[] password, byte[] salt)
应该返回byte[]
,而return toHex(hashedPass)
返回不兼容的字符串。
更改方法toHex(hashedPass)
的返回类型并返回byte[]
或
变化
来自
return toHex(hashedPass);
收件人
return toHex(hashedPass).getBytes();