Mercurial > ec-dotfiles
comparison bin/ed2k @ 4:94ee419ad047
Added bin-ec - a collection of scripts.
author | Edho Prima Arief <me@myconan.net> |
---|---|
date | Sat, 10 Jul 2010 12:38:52 +0000 |
parents | |
children | bcdf320dabf4 |
comparison
equal
deleted
inserted
replaced
3:39ef7f7236fc | 4:94ee419ad047 |
---|---|
1 #!/usr/bin/env ruby | |
2 require 'openssl' | |
3 | |
4 def file_ed2k(file_name, output_mode = "hash") | |
5 ed2k_block = 9500*1024 #ed2k block size is 9500 KiB | |
6 ed2k_hash = "" | |
7 file = File.open(file_name, 'rb') | |
8 file_size = file.stat.size #while at it, fetch the size of the file | |
9 while (block = file.read(ed2k_block)) do | |
10 ed2k_hash << OpenSSL::Digest::MD4.digest(block) #hashes are concatenated md4 per block size for ed2k hash | |
11 end | |
12 ed2k_hash << OpenSSL::Digest::MD4.digest("") if file_size % ed2k_block == 0 #on size of modulo block size, append another md4 hash of a blank string | |
13 file.close | |
14 ed2k_hash = OpenSSL::Digest::MD4.hexdigest(ed2k_hash) #finally | |
15 return case output_mode #there are 2 modes, just the has, or complete with link. | |
16 when "hash" | |
17 ed2k_hash | |
18 when "link" | |
19 "ed2k://|file|#{File.basename(file_name)}|#{file_size}|#{ed2k_hash}|" | |
20 end | |
21 end | |
22 | |
23 ARGV.each do |file_name| | |
24 next unless File.file?(file_name) and File.readable?(file_name) | |
25 printf("%s\n", file_ed2k(file_name,"link")) | |
26 end |