Created
June 17, 2018 11:01
-
-
Save Erezbiox1/722ce81b3802d9ca72b28f35d26593aa to your computer and use it in GitHub Desktop.
Create a weak hash for "Hello world" then solve it matrix style.
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
package com.erezbiox1 | |
import java.security.MessageDigest | |
import java.util.* | |
import java.util.concurrent.ThreadLocalRandom | |
/** | |
* Created by Erezbiox1 on 22/05/2018. | |
* (C) 2018 Erez Rotem All Rights Reserved. | |
*/ // 33 - 126 // 16 | |
fun main(args: Array<String>){ | |
println("Stupid matrix decoder 9000!") | |
println("Please choose if you want to decode or encode your message: ( E or D )") | |
val line = readLine()!! | |
when(line.trim().toLowerCase()){ | |
"e" -> { | |
println("Please enter your message:") | |
val message = readLine()!! | |
println(weakHash(message)) | |
} | |
"d" -> { | |
println("Please enter your message:") | |
val message = readLine()!! | |
singleBruteforce(message) | |
} | |
else -> println("Sorry that's not an option.") | |
} | |
} | |
fun singleBruteforce(weakHash: String) : String { | |
val hashedLetters = Base64.getDecoder().decode(weakHash.toByteArray()).toList().chunked(3) | |
val solvedLetters = StringBuilder() | |
for (hashedLetter in hashedLetters) { | |
var char = getRandomChar() | |
while(!char.hash().contentEquals(hashedLetter.toByteArray())){ | |
print(solvedLetters.toString()) | |
print(char) | |
println() | |
char = getRandomChar() | |
Thread.sleep(3) | |
} | |
solvedLetters.append(char) | |
} | |
val result = solvedLetters.toString() | |
println(result) | |
return result | |
} | |
fun weakHash(word: String) : String { | |
val byteList = mutableListOf<Byte>() | |
for (letter in word.toCharArray()) { | |
byteList.addAll(letter.toString().hash().toList()) | |
} | |
return Base64.getEncoder().encodeToString(byteList.toByteArray()) | |
} | |
fun String.hash(algorithm: String = "MD5") : ByteArray { | |
return MessageDigest.getInstance(algorithm).digest(this.toByteArray()).sliceArray(1..3) | |
} | |
fun getRandomChar() : String { | |
return getRandom(32..126).toChar().toString() | |
} | |
fun getRandom(range: IntRange) : Int { | |
return ThreadLocalRandom.current().nextInt(range.start, range.last + 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment