Skip to content

Instantly share code, notes, and snippets.

@luos
Created February 9, 2014 17:17
Show Gist options
  • Save luos/f2c8098be3ef0e49cee9 to your computer and use it in GitHub Desktop.
Save luos/f2c8098be3ef0e49cee9 to your computer and use it in GitHub Desktop.
import scala.sys.process._
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
import akka.actor._
import java.io.ByteArrayInputStream
import scala.sys.process.Process
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
object Scalaminer {
val CREATE_DIR = "CREATE_DIR"
val GIT_URL = "lvl1-dyu50raj@stripe-ctf.com:level1"
val GIT_USER = "user-s9zxiiv1"
val MINE = "MINE"
val TEXT = "Mining a coin. " + GIT_URL + " @ "+GIT_USER
var tree = ""
var parent = ""
val command = Seq("git", "hash-object", "-t", "commit", "--stdin")
val difficulty = "000001"
var committer : akka.actor.ActorRef = null
def main(args : Array[String]){
//"rm -rf ./*".!
"git fetch origin".!
"git reset --hard origin/master".!
"git reset --hard origin/master".!!
val fw = new java.io.FileWriter("LEDGER.txt", true) ;
fw.write("user-s9zxiiv1: 1\n") ;
fw.close()
"git add .".!
//"git commit -m\"aaaaaaaaa\"".!
println("WRITTEN")
tree = Seq("git","write-tree").!!.trim
parent = Seq("git","rev-parse","HEAD").!!.trim
println("Current tree: "+tree)
println("Current dir: " + ("pwd".lines_!))
println("Current parent" + parent)
val bode = s"tree ${tree}\n" +
s"parent ${parent}\n" +
s"author CTF user <me@example.com> ${now} +0000\n" +
"""committer CTF user <me@example.com> 1390941203 +0000
Give me a Gitcoin
"""
var eq = 0
val max = 10000000
for( i <- (0 to max).par) {
val cbode = bode + i+"\n"
val fullbode = "commit "+cbode.length+"\0" + cbode
val hash = digest(fullbode).reverse.padTo(40,"0").reverse.mkString
if (hash < difficulty){
println("-----------------------")
println(fullbode)
println("-----------------------")
val cmd = "git hash-object -t commit --stdin -w <<< \""+cbode.trim()+"\";git reset --hard \""+hash+"\" > /dev/null; git push origin master;"
println(cmd)
println("-----------------------")
println("HASH: "+hash)
println(" GITHASH: "+ commitHash(cbode))
//System.exit(0)
}
}
println("EQ: "+eq+" max: "+max)
println("END")
}
def calc(){
}
class Miner extends akka.actor.Actor{
import scala.sys.process._
var end = false
def receive = {
case (num: Int) => {
val str = body + num + "\n"
val str2 = body + num
val bytes = ( str ).getBytes("ISO-8859-2")
val inp = new ByteArrayInputStream(bytes)
val hash = commitHash(inp)
val digester = java.security.MessageDigest.getInstance("SHA-1");
digester.reset()
val dHash = digester.digest(bytes)
println(hash +" ## " + BigInt(1,dHash).toString(16))
println(hash == dHash)
if (hash < difficulty){
println("HASH FOUND "+ hash)
println("-----"+str+"----")
committer ! (str,inp)
}
}
case "kurva anyad" => {
"./miner level1 user-s9zxiiv1".!!
}
}
}
class Committer extends Actor{
var end = false
def receive = {
case (body : String, inp : ByteArrayInputStream) => {
if (!end){
end = true
println(body)
"git hash-object -t commit --stdin ".#<(inp).!!
}
}
}
}
def commitHash(str: String) : String = {
val bytes = ( str ).getBytes("ISO-8859-2")
val inp = new ByteArrayInputStream(bytes)
commitHash(inp)
}
def commitHash( inp : ByteArrayInputStream): String = {
try {
"git hash-object -t commit --stdin ".#<(inp).lines_!.headOption.getOrElse("zzz")
} catch {
case e: Exception => println(e.toString)
"error"
}
}
val now = System.currentTimeMillis() / 1000
def body : String = {
s"tree ${tree}" +
s"parent ${parent}" +
s"author CTF user <me@example.com> ${now} +0000\n" +
s"committer CTF user <me@example.com> ${now} +0000\n" +
"\nGive me a Gitcoin\n\n"
}
def dig = {
val d = java.security.MessageDigest.getInstance("SHA-1");
d.reset()
d
}
def digest(s : String) : String = { (BigInt(1,dig.digest(s.getBytes("ISO-8859-2"))) ).toString(16)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment