Skip to content

Instantly share code, notes, and snippets.

@Bregor
Created April 16, 2015 19:09
Show Gist options
  • Save Bregor/969206f411886c8c92f1 to your computer and use it in GitHub Desktop.
Save Bregor/969206f411886c8c92f1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Ruby implementation of Tor's password hashing OpenPGP S2K algorithm
require 'securerandom'
require 'digest/sha1'
secret = 'some very secret phrase'
salt = SecureRandom.random_bytes(8)
count = 65536
salted_secret = salt + secret
hash = Digest::SHA1.new
while count > 0
if count > salted_secret.length
hash.update(salted_secret)
count = count - salted_secret.length
else
hash.update(salted_secret[0...-count])
count = 0
end
end
hashed = hash.digest
salt = salt.each_byte.map { |b| b.to_s(16) }.join.upcase
indicator = 96.chr.each_byte.map { |b| b.to_s(16) }.join.upcase
torhash = hashed.each_byte.map { |b| b.to_s(16) }.join.upcase
return '16:' + salt + indicator + torhash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment