Java 不相容类型;需要和找到。但找到的是必需的类型
我收到错误“不兼容的类型。必需:字节[]。找到:java.lang.string 我已经尝试了来自的解决方案,该解决方案声明我必须初始化类型。我初始化了字节[],但仍然收到该错误Java 不相容类型;需要和找到。但找到的是必需的类型,java,android,Java,Android,我收到错误“不兼容的类型。必需:字节[]。找到:java.lang.string 我已经尝试了来自的解决方案,该解决方案声明我必须初始化类型。我初始化了字节[],但仍然收到该错误 public static byte[] hash(char[] password, byte[] salt) { PBEKeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH); Arrays.fill(password
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)
应返回byte[]
和return-toHex(hashedPass)
返回不兼容的字符串
将方法的返回类型更改为hex(hashedPass)
并返回字节[]
或
变,
从
到
return toHex(hashedPass);
return toHex(hashedPass).getBytes();