MD5的实现
scala 版本
1 def getSigned(createTime:String): String ={ 2 val secrectStr = appId+createTime+appKey 3 val digest: MessageDigest = MessageDigest.getInstance("MD5") 4 val encoded: Array[Byte] = digest.digest(secrectStr.getBytes()) 5 val str = encoded.map("%02x".format(_)).mkString 6 str 7 }
Java版本
1 import cn.hutool.http.HttpUtil; 2 3 import java.nio.charset.StandardCharsets; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 import java.util.Calendar; 7 import java.util.Locale; 8 9 /** 10 * @author lipenghuai 11 * @version 1.0 12 * @description desc 13 * @date 2022-04-29 11:33 14 */ 15 public class MD5Test { 16 private static String MD5 = "MD5"; 17 private static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 18 19 public static String sign(String str) throws NoSuchAlgorithmException { 20 byte[] btInput = str.getBytes(StandardCharsets.UTF_8); 21 MessageDigest messageDigest = MessageDigest.getInstance(MD5); 22 messageDigest.update(btInput); 23 24 byte[] encryptData = messageDigest.digest(); 25 String encryptDataStr = bytesToHex(encryptData); 26 return encryptDataStr; 27 28 } 29 30 public static String bytesToHex(byte[] bytes){ 31 int k = 0; 32 char[] hexChars = new char[bytes.length * 2]; 33 for (int i = 0; i < bytes.length; i++) { 34 byte byte0 = bytes[i]; 35 hexChars[k++] = hexDigits[byte0 >>> 4 & 0xf]; 36 hexChars[k++] = hexDigits[byte0 & 0xf]; 37 } 38 return new String(hexChars); 39 } 40 41 public static void main(String[] args) throws NoSuchAlgorithmException { 42 System.out.println(MD5Test.sign("0142ce45b6d84a2f85eafe6992ddb16e1651210819sn6852163Y8Z").toLowerCase(Locale.ROOT)); 43 System.out.println(System.currentTimeMillis() / 1000); 44 System.out.println(Calendar.getInstance().getTimeInMillis()); 45 46 } 47 }
python版本
1 def get_sign(self): 2 str = self.__app_id+self.__create_time+self.__app_key 3 md5 = hashlib.md5() 4 md5.update(str.encode("utf-8")) 5 return md5.hexdigest()
有朝一日同风起,扶摇直上九万里