Skip to content

Instantly share code, notes, and snippets.

@helloitszak
Last active August 1, 2016 20:11
Show Gist options
  • Save helloitszak/6807214 to your computer and use it in GitHub Desktop.
Save helloitszak/6807214 to your computer and use it in GitHub Desktop.
This quickly gets ed2k links for anidb crequing.
#!/usr/bin/env ruby
require 'openssl'
if ARGV.length == 0
puts "This quickly gets ed2k links for anidb crequing."
puts "Usage: #{$0} [files]"
exit
end
def ed2k(file_name)
ed2k_block = 9500 * 1024
ed2k_hash = ""
file_size = nil
File.open(file_name, 'rb') do |f|
file_size = f.stat.size #while at it, fetch the size of the file
while block = f.read(ed2k_block) do
#hashes are concatenated md4 per block size for ed2k hash
ed2k_hash << OpenSSL::Digest::MD4.digest(block)
end
#on size of modulo block size, append another md4 hash of a blank string
ed2k_hash << OpenSSL::Digest::MD4.digest("") if(file_size % ed2k_block) == 0
end
#finally
ed2k_hash = OpenSSL::Digest::MD4.hexdigest(ed2k_hash)
[ file_size, ed2k_hash ]
end
# ed2k sample link: ed2k://|file|The_Two_Towers-The_Purist_Edit-Trailer.avi|14997504|965c013e991ee246d63d45ea71954c4d|/
ARGV.each do |file|
filebase = File.basename(file)
size, hash = ed2k(file)
puts "ed2k://|file|#{filebase}|#{size}|#{hash}|/"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment