Skip to content

Instantly share code, notes, and snippets.

@lbmn
Created April 17, 2017 02:33
Show Gist options
  • Select an option

  • Save lbmn/d2b8f8d15a7bd50df85621059cb80ed3 to your computer and use it in GitHub Desktop.

Select an option

Save lbmn/d2b8f8d15a7bd50df85621059cb80ed3 to your computer and use it in GitHub Desktop.
import
os, nre, times, strutils, threadpool
when defined(securehash):
echo "(using securehash)"
import securehash
else:
echo "(using sha1)"
import sha1
type
BruteHolder = object
best: int
total: int
base: string
let
startTime = epochTime()
proc getHashStr(s: string): string {.inline.} =
when defined(securehash):
return $secureHash(s)
else:
return sha1.compute(s).toHex()
proc countLeading(input: string, target: char): int {.inline.} =
var curChr = 0
while input[curChr] == target:
inc curChr
return curChr
proc hashPerSec(interval: float, count: int): string {.inline.} =
let
rateInt = (count.float / interval).int
rateStr = ($rateInt).replace(re"(\d)(?=(\d{3})+(?!\d))", "$1,")
return $rateStr.align(10)
proc bruteForce(start, increment: int, holder: ptr BruteHolder) {.thread.} =
var
best = -1
index = start
c: int = 0
timeDiff: float
echo "bruteForce started (start=$1, increment=$2)" %
[$start, $increment]
while true:
let
hashIn = holder.base & $index
hashOut = getHashStr(hashIn)
c = countLeading(hashOut, '0')
if c > holder.best:
holder.best = c
timeDiff = cast[float](epochTime() - startTime)
let hashRateStr = hashPerSec(timeDiff, holder.total)
echo "$1 H/s $2($3) '$4'" %
[hashRateStr, hashOut, $holder.best, hashIn]
inc index, increment
inc holder.total
when isMainModule:
if paramCount() != 2:
echo "Usage :\n\t./nim-bruteforce <prefix> <number of threads>"
quit()
let threadNum = paramStr(2).parseInt
var holder: BruteHolder
holder.base = paramStr(1)
for i in 0..<threadNum:
echo "(spawning thread #$1 of $2)" % [$i, $threadNum]
spawn bruteForce(i, threadNum, addr holder)
echo "(all spawned)"
sync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment