-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion04.kt
50 lines (38 loc) · 1.35 KB
/
question04.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.File
import java.io.FileOutputStream
import java.security.MessageDigest
import kotlin.system.measureTimeMillis
fun main(args: Array<String>) {
val inputFile = File(args[0])
val outputFile = File(args[1])
val md5Hash = calculateHash(inputFile, "MD5")
val sha1Hash = calculateHash(inputFile, "SHA-1")
val sha256Hash = calculateHash(inputFile, "SHA-256")
val outputString = """
MD5: $md5Hash
SHA-1: $sha1Hash
SHA-256: $sha256Hash
""".trimIndent()
writeToFile(outputFile, outputString)
}
fun calculateHash(file: File, algorithm: String): String {
val startTime = System.currentTimeMillis()
measureTimeMillis {
val messageDigest = MessageDigest.getInstance(algorithm)
val inputStream = file.inputStream()
val buffer = ByteArray(4096)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
messageDigest.update(buffer, 0, bytesRead)
}
val bytes = messageDigest.digest()
val hexString = bytes.joinToString("") { "%02x".format(it) }
println("$algorithm calculated in ${System.currentTimeMillis() - startTime} ms")
return hexString
}
}
fun writeToFile(file: File, content: String) {
FileOutputStream(file).use { output ->
output.write(content.toByteArray())
}
}