aboutsummaryrefslogtreecommitdiffstats
path: root/hash.js
blob: 17ecd9611370d833d3d2b5744fc001d3a8ec8dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// PLUGIN_INFO//{{{
var PLUGIN_INFO =
<VimperatorPlugin>
    <name>{NAME}</name>
    <description>hash of file</description>
    <author mail="konbu.komuro@gmail.com" homepage="http://d.hatena.ne.jp/hogelog/">hogelog</author>
    <version>0.2.2</version>
    <minVersion>2.3</minVersion>
    <maxVersion>2.3</maxVersion>
    <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/hash.js</updateURL>
    <detail><![CDATA[

== COMMANDS ==
hash:
    :hash md2|md5|sha1|sha256|sha384|sha512 file-path

]]></detail>
</VimperatorPlugin>;
//}}}

(function() {
    const PR_UINT_MAX = 0xffffffff;
    let Crypt = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
    let Algos = [
        ["md2", "MD2 Algorithm"],
        ["md5", "MD5 Algorithm"],
        ["sha1", "SHA1 Algorithm"],     // SHA-1
        ["sha256", "SHA256 Algorithm"], // SHA-256
        ["sha384", "SHA384 Algorithm"], // SHA-384
        ["sha512", "SHA512 Algorithm"], // SHA-512
    ];

    function getStream(path)
    {
        let file = io.File(path);
        let stream = Cc["@mozilla.org/network/file-input-stream;1"]
            .createInstance(Ci.nsIFileInputStream);
        stream.init(file, 0x01, 0444, 0);
        return stream;
    }

    // return the two-digit hexadecimal code for a byte
    function toHexString(charCode)
    {
      return ("0" + charCode.toString(16)).slice(-2);
    }


    commands.addUserCommand(["hash"], "hash of file",
        function(args){
            if (args.length!=2) {

                liberator.echo("usage \":hash md2|md5|sha1|sha256|sha384|sha512 file-path\"");
                return false;
            }
            let [algo, path] = args;
            let stream = getStream(path);

            Crypt.initWithString(algo);

            // read the entire stream
            Crypt.updateFromStream(stream, PR_UINT_MAX);

            stream.close();

            // get base-64 string
            let hash = Crypt.finish(false);

            // convert the binary hash data to a hex string.
            let str = [toHexString(hash.charCodeAt(i)) for(i in hash)].join("");
            util.copyToClipboard(str, true);
        },
        {
            bang: true,
            completer: function (context, args){
                if (args.completeArg == 0) {
                    context.title = ["hash", "algorithm"];
                    context.completions = Algos;
                } else if (args.completeArg == 1) {
                    completion.url(context, "f");
                }
            },
            literal: 1,
        },
        true
        );

})();
// vim: fdm=marker sw=4 ts=4 et:
998;' +'background-color: '+sl_bg_color+';' +'filter: alpha(opacity=0);' +'-moz-opacity: 0.0;' +'opacity: 0.0;';", "document.body.innerHTML += '<div id=\"__sl_background__\" style=\"'+sl_style_background+'\"><br></div>';", 'var sl_bg_counter = 0;', 'sl_open = function() {', ' var oid = "__sl_background__";', ' var op = sl_bg_counter;', ' var ua = navigator.userAgent', " document.getElementById(oid).style.filter = 'alpha(opacity=' + (op * 10) + ')';", ' document.getElementById(oid).style.MozOpacity = op / 10;', ' document.getElementById(oid).style.opacity = op / 10;', ' if ( sl_bg_counter < 8 ) {', ' sl_bg_counter++;', " setTimeout('sl_open()',100);", ' } else {', ' sl_run();', ' }}', 'sl_run = function() {', ' document.getElementById("__sl_main__").innerHTML = sl_patterns[sl_counter];', ' document.getElementById("__sl_main__").style.left = windowWidth - sl_position + "px";', ' if (sl_counter < 5) {sl_counter++;} else {sl_counter=0;}', ' sl_position += sl_pitch;', ' if ( sl_w + (windowWidth - sl_position) < 0 ) {', ' sl_counter = 0;', ' sl_position = 0;', ' document.body.removeChild(document.getElementById("__sl_main__"));', ' sl_close();', ' } else {', " setTimeout('sl_run()',sl_speed);", ' }}', 'sl_close = function() {', ' var oid = "__sl_background__";', ' var op = sl_bg_counter;', ' var ua = navigator.userAgent', ' document.getElementById(oid).style.MozOpacity = op / 10;', ' if ( sl_bg_counter > 0 ) {', ' sl_bg_counter--;', " setTimeout('sl_close()',100);", ' } else {', ' document.body.removeChild(document.getElementById(oid));', ' }}', 'sl_open();'].join('\n'); //}}} function sl(){ //'var sl_speed=100,sl_pitch=15,sl_tx_color="#FFFFFF",sl_bg_color="#000000",sl_steam=[];' , var option = { sl_speed : 100, sl_pitch : 15, sl_fg_color : "#FFFFFF", sl_bg_color : "#000000" }; for (let v in option){ if (liberator.globalVariables[v]) option[v] = liberator.globalVariables[v]; } var option_code = "var sl_speed=" + option.sl_speed + ",sl_pitch=" + option.sl_pitch + ",sl_tx_color=\"" + option.sl_fg_color + "\"" + ",sl_bg_color=\"" + option.sl_bg_color + "\";\n"; var script = d.createElement('script'); script.setAttribute('type','application/javascript'); var cdata = document.createCDATASection(option_code + data); //var cdata = d.createComment(data); script.appendChild(cdata); liberator.echo('\n\n\n\n\n\n\n\n\n\n\n\n\n',true); d.body.appendChild(script); }; var orig_beep = liberator.beep; liberator.beep = function(){ if (liberator.options.sl_beep) sl(); else orig_beep(); }; return sl; })();