• Java:md5 摘要


    环境

    1. jdk 8
    2. commons-codec 1.14
    3. 4.3.24.RELEASE

    示例

    jdk

    package jiangbo.java.lang;
    
    import java.io.IOException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    import javax.xml.bind.DatatypeConverter;
    
    public class Md5Demo {
    
        public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
    
            String str = "jiangbo";
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] digestBytes = messageDigest.digest(str.getBytes("UTF-8"));
            String md5String = DatatypeConverter.printHexBinary(digestBytes);
            System.out.println(md5String);
        }
    }
    

    commons-codec

    加入依赖

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.14</version>
    </dependency>
    
    package jiangbo.java.lang;
    
    import java.util.Locale;
    
    import org.apache.commons.codec.digest.DigestUtils;
    
    public class Md5Demo {
    
        public static void main(String[] args) {
    
            String str = "jiangbo";
            String md5Hex = DigestUtils.md5Hex(str).toUpperCase(Locale.US);
            System.out.println(md5Hex);
        }
    }
    

    spring

    加入依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.24.RELEASE</version>
    </dependency>
    
    package jiangbo.java.lang;
    
    import java.nio.charset.StandardCharsets;
    import java.util.Locale;
    
    import org.springframework.util.DigestUtils;
    
    public class Md5Demo {
    
        public static void main(String[] args) {
    
            String str = "jiangbo";
    
    //      jdk 6
    //      Charset utf8 = Charset.forName("utf8");
    //      String hex = DigestUtils.md5DigestAsHex(str.getBytes(utf8));
    
            String hex = DigestUtils.md5DigestAsHex(str.getBytes(StandardCharsets.UTF_8));
            System.out.println(hex.toUpperCase(Locale.US));
        }
    }
    

    工具类

    package jiangbo.java.lang;
    
    import java.nio.charset.StandardCharsets;
    import java.util.Locale;
    
    import org.springframework.util.DigestUtils;
    
    public class Md5Utils {
    
        public static final String digestAsHex(String str) {
    
            return DigestUtils.md5DigestAsHex(str.getBytes(StandardCharsets.UTF_8));
        }
    
        public static final String digestAsUpperCaseHex(String str) {
    
            return digestAsHex(str).toUpperCase(Locale.US);
        }
    }
    
  • 相关阅读:
    HTTPS 深入浅出
    Elasticsearch Analyzer 的内部机制
    Elasticsearch 查看token分析过程
    elasticsearch教程大全
    【DDD】领域驱动设计实践 —— 框架实现
    阿里盒马领域驱动设计实践
    kubernetic
    安装k8s dashboard
    单机版kubernetes1.13安装
    Kubernetes踩坑记录
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/12829951.html
Copyright © 2020-2023  润新知