golang、java和php实现32位md5加密

php版本如下

java版本如下:

package com.yarm.task.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncodeUtils {
    /**
     * md5加密
     * @return
     */
    public String md5(String str){
        if(StringUtils.isBlank(str)) return str;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(str.getBytes("utf-8"));
            return toHex(bytes);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private static String toHex(byte[] bytes) {

        final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i=0; i> 4) & 0x0f]);
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }

    /**
     * java默认
     * @param str
     * @return
     */
    public final static String md5Default(String str) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(str.getBytes());
            byte b[] = md5.digest();

            StringBuffer sb = new StringBuffer("");
            for (int n = 0; n < b.length; n++) {
                int i = b[n];
                if (i < 0) i += 256;
                if (i < 16) sb.append("0");
                sb.append(Integer.toHexString(i));
            }
            return sb.toString();  //32位加密
        } catch (NoSuchAlgorithmException e) {
            // ignore
        }
        return null;
    }

    public static void main(String[] args) {
        String secret = md5Default("Shanghai");
        System.out.println("java用md5加密32位:" + secret);
    }
}

golang用md5加密结果

1.加密方法

package go_base

import (
	"crypto/md5"
	"encoding/hex"
)

func Md5(str string) (encode string) {
	md5 := md5.New()
	md5.Write([]byte(str))
	return hex.EncodeToString(md5.Sum(nil))
}

2.main函数测试

package main

import (
	"fmt"
	go_base "go-learning/go-base"
)

func main() {

	// go_base.BaseType()

	encode := go_base.Md5("Shanghai")
	fmt.Println("golang用md5加密32位:", encode)
}

加密结果:

输入:Shanghai

输出:

golang用md5加密32位: 5466ee572bcbc75830d044e66ab429bc

java用md5加密32位:5466ee572bcbc75830d044e66ab429bc

php:5466ee572bcbc75830d044e66ab429bc

你可能感兴趣的:(后端)