`
萧_瑟
  • 浏览: 157701 次
社区版块
存档分类
最新评论

MD5工具类应用

    博客分类:
  • Flex
阅读更多

1. MD5的加密工具类

2. 来自:as3-core-lib: http://code.google.com/p/as3corelib/

3. 用法:var encryptedStr:String = MD5.hash(rawstring);

 

package com.adobe.crypto {
   
    import com.adobe.utils.IntUtil;
    import flash.utils.ByteArray;   
   
    public class MD5 {
       
        public static var digest:ByteArray;
       
         
        public static function hash(s:String) :String{
            //Convert to byteArray and send through hashBinary function
            // so as to only have complex code in one location
            var ba:ByteArray = new ByteArray();
            ba.writeUTFBytes(s);   
            return hashBinary(ba);
        }
       
        public static function hashBytes(s:ByteArray) :String{   
            return hashBinary(s);
        }
       
             
        public static function hashBinary( s:ByteArray ):String {
            // initialize the md buffers
            var a:int = 1732584193;
            var b:int = -271733879;
            var c:int = -1732584194;
            var d:int = 271733878;
           
            // variables to store previous values
            var aa:int;
            var bb:int;
            var cc:int;
            var dd:int;
           
            // create the blocks from the string and
            // save the length as a local var to reduce
            // lookup in the loop below
            var x:Array = createBlocks( s );
            var len:int = x.length;
           
            // loop over all of the blocks
            for ( var i:int = 0; i < len; i += 16) {
                // save previous values
                aa = a;
                bb = b;
                cc = c;
                dd = d;               
               
                // Round 1
                a = ff( a, b, c, d, x[int(i+ 0)],  7, -680876936 );     // 1
                d = ff( d, a, b, c, x[int(i+ 1)], 12, -389564586 );    // 2
                c = ff( c, d, a, b, x[int(i+ 2)], 17, 606105819 );     // 3
                b = ff( b, c, d, a, x[int(i+ 3)], 22, -1044525330 );    // 4
                a = ff( a, b, c, d, x[int(i+ 4)],  7, -176418897 );     // 5
                d = ff( d, a, b, c, x[int(i+ 5)], 12, 1200080426 );     // 6
                c = ff( c, d, a, b, x[int(i+ 6)], 17, -1473231341 );    // 7
                b = ff( b, c, d, a, x[int(i+ 7)], 22, -45705983 );     // 8
                a = ff( a, b, c, d, x[int(i+ 8)],  7, 1770035416 );     // 9
                d = ff( d, a, b, c, x[int(i+ 9)], 12, -1958414417 );    // 10
                c = ff( c, d, a, b, x[int(i+10)], 17, -42063 );         // 11
                b = ff( b, c, d, a, x[int(i+11)], 22, -1990404162 );    // 12
                a = ff( a, b, c, d, x[int(i+12)],  7, 1804603682 );     // 13
                d = ff( d, a, b, c, x[int(i+13)], 12, -40341101 );     // 14
                c = ff( c, d, a, b, x[int(i+14)], 17, -1502002290 );    // 15
                b = ff( b, c, d, a, x[int(i+15)], 22, 1236535329 );     // 16
               
                // Round 2
                a = gg( a, b, c, d, x[int(i+ 1)],  5, -165796510 );     // 17
                d = gg( d, a, b, c, x[int(i+ 6)],  9, -1069501632 );    // 18
                c = gg( c, d, a, b, x[int(i+11)], 14, 643717713 );     // 19
                b = gg( b, c, d, a, x[int(i+ 0)], 20, -373897302 );     // 20
                a = gg( a, b, c, d, x[int(i+ 5)],  5, -701558691 );     // 21
                d = gg( d, a, b, c, x[int(i+10)],  9, 38016083 );     // 22
                c = gg( c, d, a, b, x[int(i+15)], 14, -660478335 );     // 23
                b = gg( b, c, d, a, x[int(i+ 4)], 20, -405537848 );     // 24
                a = gg( a, b, c, d, x[int(i+ 9)],  5, 568446438 );     // 25
                d = gg( d, a, b, c, x[int(i+14)],  9, -1019803690 );    // 26
                c = gg( c, d, a, b, x[int(i+ 3)], 14, -187363961 );     // 27
                b = gg( b, c, d, a, x[int(i+ 8)], 20, 1163531501 );     // 28
                a = gg( a, b, c, d, x[int(i+13)],  5, -1444681467 );    // 29
                d = gg( d, a, b, c, x[int(i+ 2)],  9, -51403784 );     // 30
                c = gg( c, d, a, b, x[int(i+ 7)], 14, 1735328473 );     // 31
                b = gg( b, c, d, a, x[int(i+12)], 20, -1926607734 );    // 32
               
                // Round 3
                a = hh( a, b, c, d, x[int(i+ 5)],  4, -378558 );     // 33
                d = hh( d, a, b, c, x[int(i+ 8)], 11, -2022574463 );    // 34
                c = hh( c, d, a, b, x[int(i+11)], 16, 1839030562 );     // 35
                b = hh( b, c, d, a, x[int(i+14)], 23, -35309556 );     // 36
                a = hh( a, b, c, d, x[int(i+ 1)],  4, -1530992060 );    // 37
                d = hh( d, a, b, c, x[int(i+ 4)], 11, 1272893353 );     // 38
                c = hh( c, d, a, b, x[int(i+ 7)], 16, -155497632 );     // 39
                b = hh( b, c, d, a, x[int(i+10)], 23, -1094730640 );    // 40
                a = hh( a, b, c, d, x[int(i+13)],  4, 681279174 );     // 41
                d = hh( d, a, b, c, x[int(i+ 0)], 11, -358537222 );     // 42
                c = hh( c, d, a, b, x[int(i+ 3)], 16, -722521979 );     // 43
                b = hh( b, c, d, a, x[int(i+ 6)], 23, 76029189 );     // 44
                a = hh( a, b, c, d, x[int(i+ 9)],  4, -640364487 );     // 45
                d = hh( d, a, b, c, x[int(i+12)], 11, -421815835 );     // 46
                c = hh( c, d, a, b, x[int(i+15)], 16, 530742520 );     // 47
                b = hh( b, c, d, a, x[int(i+ 2)], 23, -995338651 );     // 48
               
                // Round 4
                a = ii( a, b, c, d, x[int(i+ 0)],  6, -198630844 );     // 49
                d = ii( d, a, b, c, x[int(i+ 7)], 10, 1126891415 );     // 50
                c = ii( c, d, a, b, x[int(i+14)], 15, -1416354905 );    // 51
                b = ii( b, c, d, a, x[int(i+ 5)], 21, -57434055 );     // 52
                a = ii( a, b, c, d, x[int(i+12)],  6, 1700485571 );     // 53
                d = ii( d, a, b, c, x[int(i+ 3)], 10, -1894986606 );    // 54
                c = ii( c, d, a, b, x[int(i+10)], 15, -1051523 );     // 55
                b = ii( b, c, d, a, x[int(i+ 1)], 21, -2054922799 );    // 56
                a = ii( a, b, c, d, x[int(i+ 8)],  6, 1873313359 );     // 57
                d = ii( d, a, b, c, x[int(i+15)], 10, -30611744 );     // 58
                c = ii( c, d, a, b, x[int(i+ 6)], 15, -1560198380 );    // 59
                b = ii( b, c, d, a, x[int(i+13)], 21, 1309151649 );     // 60
                a = ii( a, b, c, d, x[int(i+ 4)],  6, -145523070 );     // 61
                d = ii( d, a, b, c, x[int(i+11)], 10, -1120210379 );    // 62
                c = ii( c, d, a, b, x[int(i+ 2)], 15, 718787259 );     // 63
                b = ii( b, c, d, a, x[int(i+ 9)], 21, -343485551 );     // 64

                a += aa;
                b += bb;
                c += cc;
                d += dd;
            }
            digest = new ByteArray()
            digest.writeInt(a);
            digest.writeInt(b);
            digest.writeInt(c);
            digest.writeInt(d);
            digest.position = 0;
            // Finish up by concatening the buffers with their hex output
            return IntUtil.toHex( a ) + IntUtil.toHex( b ) + IntUtil.toHex( c ) + IntUtil.toHex( d );
        }
       
       
        private static function f( x:int, y:int, z:int ):int {
            return ( x & y ) | ( (~x) & z );
        }
       
       
        private static function g( x:int, y:int, z:int ):int {
            return ( x & z ) | ( y & (~z) );
        }
       
       
        private static function h( x:int, y:int, z:int ):int {
            return x ^ y ^ z;
        }
       
       
        private static function i( x:int, y:int, z:int ):int {
            return y ^ ( x | (~z) );
        }
       
       
        private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int {
            var tmp:int = a + int( func( b, c, d ) ) + x + t;
            return IntUtil.rol( tmp, s ) +  b;
        }
       
       
        private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
            return transform( f, a, b, c, d, x, s, t );
        }
       
       
        private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
            return transform( g, a, b, c, d, x, s, t );
        }
       
       
        private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
            return transform( h, a, b, c, d, x, s, t );
        }
       
       
        private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
            return transform( i, a, b, c, d, x, s, t );
        }
       
       
        private static function createBlocks( s:ByteArray ):Array {
            var blocks:Array = new Array();
            var len:int = s.length * 8;
            var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
            for( var i:int = 0; i < len; i += 8 ) {
                blocks[ int(i >> 5) ] |= ( s[ i / 8 ] & mask ) << ( i % 32 );
            }
           
            // append padding and length
            blocks[ int(len >> 5) ] |= 0x80 << ( len % 32 );
            blocks[ int(( ( ( len + 64 ) >>> 9 ) << 4 ) + 14) ] = len;
            return blocks;
        }
       
    }
}

 

4. java中直接使用 MessageDigest 类

public static String md5(String str) {
        String s = str;
        if (s == null) {
            return "";
        } else {
            System.out.println(s);
            String value = null;
            MessageDigest md5 = null;
            try {
                md5 = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            @SuppressWarnings("restriction")
            sun.misc.BASE64Encoder baseEncoder = new sun.misc.BASE64Encoder();
            try {
                @SuppressWarnings("restriction")
                String encode = baseEncoder.encode(md5.digest(s.getBytes("utf-8")));
                value = encode;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return value;
        }
    }

 

5.  http://blog.sina.com.cn/s/blog_674522cd01010wnd.html

分享到:
评论

相关推荐

    MD5加密工具类

    MD5加密算法,主要对一段信息(Message)产生信息摘要(Message-Digest),以防止被篡改。利用MD5算法来进行文件校验的方案被大量应用到软件下载站、论坛数据库、系统文件安全等方面。

    jsp调用MD5加密

    MD5(Message-Digest Algorithm 5 信息摘要算法),是一种不可逆的加密算法,主要被应用于确保信息传输的完整一致性与系统登录认证这两方面。

    md5加密工具类

    此为简单的md5加密,可为初学者应用,可以直接调用,希望能用

    Java实现的Base64、MD5、3DES三种工具

    在Java中,MD5加密工具可以使用java.security.MessageDigest类来实现。MessageDigest类提供了一个update()方法来更新要加密的数据,并提供了一个digest()方法来获取加密后的散列值。 在本文档的示例代码中,我们...

    自己收集整理的一些常用的工具类

    DatabaseExportUtils 应用数据库导出工具类 DateUtil 日期操作工具类 DbHelper 数据库帮助类 DensityUtil 屏幕信息获取数值的转换 DeviceStatusUtils 手机状态工具类 主要包括网络、蓝牙、屏幕亮度、飞行模式、音量...

    百度翻译API工具类

    百度翻译API工具类,提供了百度翻译的工具类的部分代码。用于移动应用开发或者Java应用开发,本类中包括http请求类,md5 加密类,TransApi类

    java基础类库开发包,工作5年精心整理_Java常用工具类源码

    工作5年精心整理_Java常用工具类源码 收集了java项目开中常用的工具操作类,方法非常全,可应用在大部份java 项目中。 提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档...

    VS2010求值MD5的实例代码

    完整代码,点击sln可以打开工程,并在VS2010上调试成功,对初学者来说简单易懂,还可以作为工具类计算MD5,很实用的小应用。

    MD5加密技术

    疑问:MD5加密技术已被很多的项目软件工程所使用,EncryptionTool类中没有“密钥”属性,那么全世界使用MD5加密技术开发的软件中加密同一个字符串得到的加密后的字符串岂不是一样的吗? 这种认识是不对的,采用MD5...

    推荐一款封装各种Util工具类,这款神仙级框架你值得拥有!.docx

    2. 加密解密:Hutool 的 SecureUtil 工具类提供了加密解密的功能,例如 md5 加密,可以用于登录和密码修改时的加密处理。 3. HTML 工具类:Hutool 的 HtmlUtil 工具类提供了 HTML 编码、解码、清除 HTML 标签、过滤...

    MD5校验器 v1.0.1.2 绿色版.zip

    这款MD5校验器是一款免费的md5值校验工具,我们平时在网上下载软件总怕是别人修改过的,这种情况下就需要校验原版的md5值与下载的md5值是否相同了,若果相同则是没有修改过的,若不同则是有过修改。 MD5功能特色: ...

    懒惰:Android工具自己整理的常用的工具类

    应用数据库衍生工具类 日期操作工具类 数据库帮助类 屏幕信息获取数值的转换 手机状态工具类主要包括网络,蓝牙,屏幕亮度,飞行模式,音量等 DigestUtils 文件操作 拼音汉字处理 欠税额 MD5 设备信息的获取 网络...

    通用Android工具库Common4Android.zip

    MD5加密工具类。 RegexUtil.java 常用正则表达式工具类。 SDCardUtil.java SD卡信息管理工具类。 SharedPreferencesUtil.java ...

    md格式编写的良心教程 Python 100天从新手到大师 共100个完整源文件 含课程源代码.rar

    Day76-90\78.NumPy和SciPy的应用.md Day76-90\79.Matplotlib和数据可视化.md Day76-90\80.k最近邻分类.md Day76-90\81.决策树.md Day76-90\82.贝叶斯分类.md Day76-90\83.支持向量机.md Day76-90\84.K-均值聚类.md ...

    Python itertools模块笔记:迭代工具.md

    不仅能学会使用迭代工具处理数据,也能根据实际问题选择合适的函数应用。 阅读建议: 可以先了解整体模块介绍,然后逐节学习各函数及示例,最后结合案例,将知识应用到数据处理、算法设计等实际场景中,通过案例驱动来...

    联胜电脑装机工具箱V1.0

    《联胜电脑装机工具箱V1.0》中包含3大类功能分别如下: 一、应用维护 1.C盘转换为NTFS格式 ...7.MD5效验工具 8.文件隐藏+加密工具 9.硬件查询检测 10.硬盘使用工具 11.显示器检测工具 12.CPU检测工具

    java笔记:02.windows常用快捷键.md

    它被广泛用于开发Web应用程序、企业级应用程序、移动应用程序、嵌入式系统和消费类电子产品等。Java提供了丰富的类库和工具,使开发人员能够快速构建各种类型的应用程序。 Java语言具有面向对象的特性,包括封装、...

    java笔记:01.IDEA常用配置总结.md

    它被广泛用于开发Web应用程序、企业级应用程序、移动应用程序、嵌入式系统和消费类电子产品等。Java提供了丰富的类库和工具,使开发人员能够快速构建各种类型的应用程序。 Java语言具有面向对象的特性,包括封装、...

    java开源包7

    用来计算 MD5、SHA 哈希算法的 Java 类库,支持 "MD5", "SHA", "SHA-1", "SHA-256", "SHA-384", "SHA-512". 高性能RPC框架 nfs-rpc nfs-rpc是一个集成了各种知名通信框架的高性能RPC框架,目前其最好的性能为在采用...

Global site tag (gtag.js) - Google Analytics