Skip to content

Instantly share code, notes, and snippets.

@fritz0705
Created July 29, 2010 15:23
Show Gist options
  • Save fritz0705/498409 to your computer and use it in GitHub Desktop.
Save fritz0705/498409 to your computer and use it in GitHub Desktop.
def dm_crypt(message, key)
if key.empty?
return message
end
result = ""
i = 0
message.each_byte do |byte|
result += (byte ^ key[i % key.bytesize].ord ^ i).chr
i += 1
end
return dm_crypt(result, key[1..-1])
end
def dm_keygen(msg = nil)
complexity = 2
random = File.open("/dev/urandom")
msg = random.read(complexity) unless msg
base_key = random.read(complexity)
result = dm_crypt(msg, base_key)
Fiber.new do
loop do
File.open("/dev/null", "r+b").write(random.read(complexity * 16))
key = random.read(complexity)
while result != dm_crypt(msg, key)
key = random.read(complexity)
end
Fiber.yield key
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment