zoukankan      html  css  js  c++  java
  • 用node.js写个在Bash上对字符串进行Base64或URL的encode和decode脚本

    一:自己这段时间经常要用到Base64编码和URL编码,写个编译型语言有点麻烦干脆就用node.js弄了个,弄好后在/etc/profile里加上alias就能完成工具的配置,先上代码:

    function Base64() {
    
        if(typeof Base64._initialized == "undefined"){
        
        _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        
        // public method for encoding
        Base64.encode = function (input) {
                var output = "";
                var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
                var i = 0;
                input = _utf8Encode(input);
                while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                        enc4 = 64;
            }
            output = output +
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
                }
                return output;
        }
        
        // public method for decoding
        Base64.decode = function (input) {
                var output = "";
                var chr1, chr2, chr3;
                var enc1, enc2, enc3, enc4;
                var i = 0;
                input = input.replace(/[^A-Za-z0-9+/=]/g, "");
                while (i < input.length) {
            enc1 = _keyStr.indexOf(input.charAt(i++));
            enc2 = _keyStr.indexOf(input.charAt(i++));
            enc3 = _keyStr.indexOf(input.charAt(i++));
            enc4 = _keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                        output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                        output = output + String.fromCharCode(chr3);
            }
                }
                output = _utf8Decode(output);
                return output;
        }
        
        // private method for UTF-8 encoding
        _utf8Encode = function (string) {
                string = string.replace(/
    /g,"
    ");
                var utftext = "";
                for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                        utftext += String.fromCharCode(c);
            } else if((c > 127) && (c < 2048)) {
                        utftext += String.fromCharCode((c >> 6) | 192);
                        utftext += String.fromCharCode((c & 63) | 128);
            } else {
                        utftext += String.fromCharCode((c >> 12) | 224);
                        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                        utftext += String.fromCharCode((c & 63) | 128);
            }
            
                }
                return utftext;
        }
        
        // private method for UTF-8 decoding
        _utf8Decode = function (utftext) {
                var string = "";
                var i = 0;
                var c = c1 = c2 = 0;
                while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                        string += String.fromCharCode(c);
                        i++;
            } else if((c > 191) && (c < 224)) {
                        c2 = utftext.charCodeAt(i+1);
                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                        i += 2;
            } else {
                        c2 = utftext.charCodeAt(i+1);
                        c3 = utftext.charCodeAt(i+2);
                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                        i += 3;
            }
                }
                return string;
        }
        Base64._initialized = true;
        }
    }
    
    function URLXcoder() {
        if(typeof URLXcoder._initialized == "undefined"){
        URLXcoder.encode = encodeURIComponent;
        URLXcoder.decode = decodeURIComponent;
        URLXcoder._initialized == true;
        }
    }
    
    (function main(){
      Base64();
      URLXcoder();
      var type = process.argv[2];  // Base64 Or URLXcoder
      var mode = process.argv[3];  // Encoder Or Decoder
      var value = process.argv[4];  // message
      var out = "";
      if(mode == "en" || mode == "encode"){
        if(type == "base64"){
          out = Base64.encode(value);
        }else {
          out = URLXcoder.encode(value);
        }
      }else{
        if(type == "base64"){
          out = Base64.decode(value);
        }else {
          out = URLXcoder.decode(value);
        }
      }
      console.log(out);
    })();

    使用的时候注意字符串如果是bash的特殊字符如&等需要用转义;

    二:配置/etc/profile文件

    在最后面加上alias base64='node /home/silentdoer/JsScripts/en-decoder.js base64'
     和alias urlxcoder='node /home/silentdoer/JsScripts/en-decoder.js urlxcoder',然后source;

    三:使用

    在bash里输入urlxcoder en 好.=&^,输出结果%E5%A5%BD.%3D%26%5E

    输入base64 en 我时是,输出结果5oiR5pe25piv;

    这里第一个参数表示以什么类型编码,第二个是encode还是decode(en和encode均可),第三个就是具体的字符串了(记得特殊字符要转义,或者用""或''括起来)

  • 相关阅读:
    怎样的配色能让你的作品高端!上档次呢?
    响应式布局的9项基本原则
    SOHO设计师如何保障自己的权益
    加载动态设计--等待的艺术创作思维
    初学者所必备的设计类网站
    网页设计师该向印刷设计学习的3个规则
    [并发编程] -- 内存模型(针对JSR-133内存模型)篇
    [并发编程] -- 线程池篇
    [并发编程] -- 容器和框架篇
    [并发编程] -- 工具类篇
  • 原文地址:https://www.cnblogs.com/silentdoer/p/8795091.html
Copyright © 2011-2022 走看看