| 1 | // PLUGIN_INFO//{{{ |
|---|
| 2 | var PLUGIN_INFO = |
|---|
| 3 | <VimperatorPlugin> |
|---|
| 4 | <name>{NAME}</name> |
|---|
| 5 | <description>hash of file</description> |
|---|
| 6 | <author mail="konbu.komuro@gmail.com" homepage="http://d.hatena.ne.jp/hogelog/">hogelog</author> |
|---|
| 7 | <version>0.2.1</version> |
|---|
| 8 | <minVersion>2.0pre</minVersion> |
|---|
| 9 | <maxVersion>2.0pre</maxVersion> |
|---|
| 10 | <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/hash.js</updateURL> |
|---|
| 11 | <detail><![CDATA[ |
|---|
| 12 | |
|---|
| 13 | == COMMANDS == |
|---|
| 14 | hash: |
|---|
| 15 | :hash md2|md5|sha1|sha256|sha384|sha512 file-path |
|---|
| 16 | |
|---|
| 17 | ]]></detail> |
|---|
| 18 | </VimperatorPlugin>; |
|---|
| 19 | //}}} |
|---|
| 20 | |
|---|
| 21 | (function() { |
|---|
| 22 | const PR_UINT_MAX = 0xffffffff; |
|---|
| 23 | let Crypt = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash); |
|---|
| 24 | let Algos = [ |
|---|
| 25 | ["md2", "MD2 Algorithm"], |
|---|
| 26 | ["md5", "MD5 Algorithm"], |
|---|
| 27 | ["sha1", "SHA1 Algorithm"], // SHA-1 |
|---|
| 28 | ["sha256", "SHA256 Algorithm"], // SHA-256 |
|---|
| 29 | ["sha384", "SHA384 Algorithm"], // SHA-384 |
|---|
| 30 | ["sha512", "SHA512 Algorithm"], // SHA-512 |
|---|
| 31 | ]; |
|---|
| 32 | |
|---|
| 33 | function getStream(path) |
|---|
| 34 | { |
|---|
| 35 | let file = io.getFile(path); |
|---|
| 36 | let stream = Cc["@mozilla.org/network/file-input-stream;1"] |
|---|
| 37 | .createInstance(Ci.nsIFileInputStream); |
|---|
| 38 | stream.init(file, 0x01, 0444, 0); |
|---|
| 39 | return stream; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // return the two-digit hexadecimal code for a byte |
|---|
| 43 | function toHexString(charCode) |
|---|
| 44 | { |
|---|
| 45 | return ("0" + charCode.toString(16)).slice(-2); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | commands.addUserCommand(["hash"], "hash of file", |
|---|
| 50 | function(args){ |
|---|
| 51 | if (args.length!=2) { |
|---|
| 52 | |
|---|
| 53 | liberator.echo("usage \":hash md2|md5|sha1|sha256|sha384|sha512 file-path\""); |
|---|
| 54 | return false; |
|---|
| 55 | } |
|---|
| 56 | let [algo, path] = args; |
|---|
| 57 | let stream = getStream(path); |
|---|
| 58 | |
|---|
| 59 | Crypt.initWithString(algo); |
|---|
| 60 | |
|---|
| 61 | // read the entire stream |
|---|
| 62 | Crypt.updateFromStream(stream, PR_UINT_MAX); |
|---|
| 63 | |
|---|
| 64 | stream.close(); |
|---|
| 65 | |
|---|
| 66 | // get base-64 string |
|---|
| 67 | let hash = Crypt.finish(false); |
|---|
| 68 | |
|---|
| 69 | // convert the binary hash data to a hex string. |
|---|
| 70 | let str = [toHexString(hash.charCodeAt(i)) for(i in hash)].join(""); |
|---|
| 71 | util.copyToClipboard(str, true); |
|---|
| 72 | }, |
|---|
| 73 | { |
|---|
| 74 | bang: true, |
|---|
| 75 | completer: function (context, args){ |
|---|
| 76 | if (args.completeArg == 0) { |
|---|
| 77 | context.title = ["hash", "algorithm"]; |
|---|
| 78 | context.completions = Algos; |
|---|
| 79 | } else if (args.completeArg == 1) { |
|---|
| 80 | completion.url(context, "f"); |
|---|
| 81 | } |
|---|
| 82 | }, |
|---|
| 83 | literal: 1, |
|---|
| 84 | }); |
|---|
| 85 | |
|---|
| 86 | })(); |
|---|
| 87 | // vim: fdm=marker sw=4 ts=4 et: |
|---|