Skip to content

Commit

Permalink
[KTLN-789] Add Samples (Baeldung#725)
Browse files Browse the repository at this point in the history
* [KTLN-789] Add Samples

* [KTLN-789] Add Samples

* [KTLN-789] Add Samples
  • Loading branch information
LeoColman authored Dec 22, 2023
1 parent 241f7cd commit 436b5f1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, Baeldung! I'm a file!

0 comments on commit 436b5f1

Please sign in to comment.