Mercurial > ec-dotfiles
annotate bin/ed2k @ 139:9ff99d9bf388
Merge.
author | Edho Arief <edho@myconan.net> |
---|---|
date | Tue, 01 Nov 2011 22:41:09 +0700 |
parents | 94ee419ad047 |
children | bcdf320dabf4 |
rev | line source |
---|---|
4
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
1 #!/usr/bin/env ruby |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
2 require 'openssl' |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
3 |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
4 def file_ed2k(file_name, output_mode = "hash") |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
5 ed2k_block = 9500*1024 #ed2k block size is 9500 KiB |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
6 ed2k_hash = "" |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
7 file = File.open(file_name, 'rb') |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
8 file_size = file.stat.size #while at it, fetch the size of the file |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
9 while (block = file.read(ed2k_block)) do |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
10 ed2k_hash << OpenSSL::Digest::MD4.digest(block) #hashes are concatenated md4 per block size for ed2k hash |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
11 end |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
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 |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
13 file.close |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
14 ed2k_hash = OpenSSL::Digest::MD4.hexdigest(ed2k_hash) #finally |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
15 return case output_mode #there are 2 modes, just the has, or complete with link. |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
16 when "hash" |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
17 ed2k_hash |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
18 when "link" |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
19 "ed2k://|file|#{File.basename(file_name)}|#{file_size}|#{ed2k_hash}|" |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
20 end |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
21 end |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
22 |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
23 ARGV.each do |file_name| |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
24 next unless File.file?(file_name) and File.readable?(file_name) |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
25 printf("%s\n", file_ed2k(file_name,"link")) |
94ee419ad047
Added bin-ec - a collection of scripts.
Edho Prima Arief <me@myconan.net>
parents:
diff
changeset
|
26 end |