forked from Baeldung/kotlin-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KTLN-789] Add Samples (Baeldung#725)
* [KTLN-789] Add Samples * [KTLN-789] Add Samples * [KTLN-789] Add Samples
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
core-kotlin-modules/core-kotlin-9/src/test/kotlin/com/baeldung/md5/Md5UnitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.baeldung.md5 | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
import java.security.MessageDigest | ||
|
||
class Md5UnitTest { | ||
@Test | ||
fun `Calling extension md5 should return a md5 hash`() { | ||
val stringToBeHashed = "Hello, Baeldung!" | ||
val expectedHash = "6469a4ea9e2753755f5120beb51587f8" | ||
val calculatedHash = stringToBeHashed.md5() | ||
|
||
assertEquals(expectedHash, calculatedHash) | ||
} | ||
|
||
@Test | ||
fun `Calling extension md5 on a file should return a md5 hash`() { | ||
val fileToBeHashed = File("src/test/resources/test_md5.txt") | ||
val expectedHash = "ef948f943cdba8514ed5aab7592a904d" | ||
val calculatedHash = fileToBeHashed.md5() | ||
|
||
assertEquals(expectedHash, calculatedHash) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
fun String.md5(): String { | ||
val md = MessageDigest.getInstance("MD5") | ||
val digest = md.digest(this.toByteArray()) | ||
return digest.toHexString() | ||
} | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
fun File.md5(): String { | ||
val md = MessageDigest.getInstance("MD5") | ||
val digest = md.digest(this.readBytes()) | ||
return digest.toHexString() | ||
} |
1 change: 1 addition & 0 deletions
1
core-kotlin-modules/core-kotlin-9/src/test/resources/test_md5.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, Baeldung! I'm a file! |