package com.cn.test.rsa; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Random; public class GenerServerDigest { //生成32位随机数 public static String generatorRandom(){ String _RndData; int b = 0; int a = 0; _RndData = ""; Random r = new Random(); for (int i = 0; i < 32; i++) { a = r.nextInt(26); b = (char) (a + 65); _RndData += new Character((char) b).toString(); } return _RndData; } public static void main(String[] args) { String ServerDigest = null; try{ //进行SHA1运算 char[] HEXCHAR = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ }; String MsgtoDigest = GenerServerDigest.generatorRandom(); MessageDigest md = null; md = MessageDigest.getInstance("SHA1"); byte[] digest = md.digest(MsgtoDigest.getBytes()); StringBuilder sb = new StringBuilder(digest.length * 2); for (int i = 0; i < digest.length; i++) { sb.append(HEXCHAR[(digest[i] & 0xf0) >>> 4]); sb.append(HEXCHAR[digest[i] & 0x0f]); } ServerDigest = sb.toString(); } catch (NoSuchAlgorithmException e){ e.printStackTrace(); } System.out.println(ServerDigest); } }
时间: 2024-10-23 05:40:44