这里面包含了一百多个JAVA源文件

源代码在线查看: e469. generating a message authentication code (mac).txt

软件大小: 551 K
上传用户: maple_78
关键词: JAVA
下载地址: 免注册下载 普通下载 VIP

相关代码

				A MAC is like hash code for a sequence of bytes. Unlike a hash code, a MAC uses a secret key to generate the hash code, or more specifically, the message digest. A MAC is generally used to check the integrity or validity of information based on a secret key. 
				    try {
				        // Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104
				        // In practice, you would save this key.
				        KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
				        SecretKey key = keyGen.generateKey();
				    
				        // Create a MAC object using HMAC-MD5 and initialize with key
				        Mac mac = Mac.getInstance(key.getAlgorithm());
				        mac.init(key);
				    
				        String str = "This message will be digested";
				    
				        // Encode the string into bytes using utf-8 and digest it
				        byte[] utf8 = str.getBytes("UTF8");
				        byte[] digest = mac.doFinal(utf8);
				    
				        // If desired, convert the digest into a string
				        String digestB64 = new sun.misc.BASE64Encoder().encode(digest);
				    } catch (InvalidKeyException e) {
				    } catch (NoSuchAlgorithmException e) {
				    } catch (UnsupportedEncodingException e) {
				    }
				
				
							

相关资源