我收到错误“不兼容的类型。需要:字节 []。找到:java.lang.string
我已经尝试了不兼容类型的解决方案,发现和要求相同,这表明我必须初始化类型。我初始化了字节 [] 但我仍然收到那个错误
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);
该方法hash(char[] password, byte[] salt)
应该返回 abyte[]
并return toHex(hashedPass)
返回不兼容的 String 。
改变方法的返回类型toHex(hashedPass)
并返回byte[]
或改变,
从
return toHex(hashedPass);
至
return toHex(hashedPass).getBytes();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句