Hi,
I wanted to write a Julia password checker following the news about a list of hacked passwords, posted here as a list of SHA1 hashes:
The code I came up with is this: (requires 6.2 GB RAM on the real password list)
using SHA
const N = 307_000_000
struct PWHash
data::NTuple{20,UInt8}
end
PWHash(sha_hash) = PWHash((sha_hash...))
PWHash() = PWHash((fill(typemax(UInt8),20)...))
Base.isless(x::PWHash, y::PWHash) = (x.data < y.data)
tic()
hashes = fill(PWHash(), N)
t = toc()
println("Memory allocated")
tic()
open("pwned-passwords-1.0.txt") do f
for (i,ln) in enumerate(eachline(f))
hashes[i] = PWHash(hex2bytes(ln))
end
end
t = toc()
println("File read")
pwhash = PWHash()
emptyhash = PWHash(sha1(""))
while pwhash != emptyhash
println("enter password:")
pwhash = PWHash(sha1(readline()))
if !isempty(searchsorted(hashes, pwhash))
println("You're pwned!")
else
println("You're safe, possibly")
end
end
Of course I am certain this is already perfect and couldn’t possibly be improved upon ( ), except for a small detail: the readline
echos back the password to the screen, is there any way to avoid that?