-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.scala
More file actions
45 lines (36 loc) · 1.27 KB
/
solution.scala
File metadata and controls
45 lines (36 loc) · 1.27 KB
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
import java.lang.StringBuilder
val md = java.security.MessageDigest.getInstance("MD5")
def hash(str: String): String = md.digest(str.getBytes).map("%02x".format(_)).mkString
val start = System.currentTimeMillis()
// Part 1
def encrypt1(str: String, i: Int, curr: String): String = {
if(curr.length >= 8) { curr }
if(i % 100000 == 0){
val time = (System.currentTimeMillis() - start) / 1000
println(s"Encrypting $i... Time passed: $time seconds. Current pw: $curr")
}
val h = hash(str + i)
val res = if(h.substring(0,5) == "00000"){ curr + h(5) } else { curr }
encrypt1(str, i + 1, res)
}
// Part 2
def encrypt2(str: String, i: Int, curr: String): String = {
if(curr.indexOf('-') < 0) { curr }
if(i % 100000 == 0){
val time = (System.currentTimeMillis() - start) / 1000
println(s"Encrypting $i... Time passed: $time seconds. Current pw: $curr")
}
val h = hash(str + i)
val res = if(h.substring(0,5) == "00000"){
println("Hej:" + h(5) + ", " + h(6))
val sb = new StringBuilder(curr)
if(h(5).asDigit < 8 && curr(h(5).asDigit) == '-'){
sb.setCharAt(h(5).asDigit, h(6))
}
sb.toString
} else { curr }
encrypt2(str, i + 1, res)
}
val input = "cxdnnyjw"
val pw = encrypt2(input, 2500000, "--------")
println(s"Password is: $pw")