import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
import java.security.SecureRandom;
import static org.assertj.core.api.Assertions.assertThat;
String random = randomString();
assertThat(hash(random)).as(random).isEqualTo(
DigestUtils.md5Hex(random).toLowerCase()
- );
+ );
}
}
+ @Test
+ public void test_hash_file() throws IOException {
+ File f = temp.newFile();
+ Files.write(f.toPath(), "sonar".getBytes(StandardCharsets.UTF_8));
+ assertThat(hashFile(f)).isEqualTo("d85e336d61f5344395c42126fac239bc");
+ }
+
@Test
public void test_toHex() {
// lower-case
String random = randomString();
assertThat(FileHashes.toHex(random.getBytes())).as(random).isEqualTo(
Hex.encodeHexString(random.getBytes()).toLowerCase()
- );
+ );
}
}
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Fail to compute hash of: " + file.getAbsolutePath());
-
new FileHashes().of(file);
}
IOUtils.closeQuietly(in);
}
}
+
+ private String hashFile(File f) {
+ return new FileHashes().of(f);
+ }
}