1 18 19 package jcifs.util; 20 21 import java.security.MessageDigest ; 22 23 29 public class HMACT64 extends MessageDigest implements Cloneable { 30 31 private static final int BLOCK_LENGTH = 64; 32 33 private static final byte IPAD = (byte) 0x36; 34 35 private static final byte OPAD = (byte) 0x5c; 36 37 private MessageDigest md5; 38 39 private byte[] ipad = new byte[BLOCK_LENGTH]; 40 41 private byte[] opad = new byte[BLOCK_LENGTH]; 42 43 48 public HMACT64(byte[] key) { 49 super("HMACT64"); 50 int length = Math.min(key.length, BLOCK_LENGTH); 51 for (int i = 0; i < length; i++) { 52 ipad[i] = (byte) (key[i] ^ IPAD); 53 opad[i] = (byte) (key[i] ^ OPAD); 54 } 55 for (int i = length; i < BLOCK_LENGTH; i++) { 56 ipad[i] = IPAD; 57 opad[i] = OPAD; 58 } 59 try { 60 md5 = MessageDigest.getInstance("MD5"); 61 } catch (Exception ex) { 62 throw new IllegalStateException (ex.getMessage()); 63 } 64 engineReset(); 65 } 66 67 private HMACT64(HMACT64 hmac) throws CloneNotSupportedException { 68 super("HMACT64"); 69 this.ipad = hmac.ipad; 70 this.opad = hmac.opad; 71 this.md5 = (MessageDigest ) hmac.md5.clone(); 72 } 73 74 public Object clone() { 75 try { 76 return new HMACT64(this); 77 } catch (CloneNotSupportedException ex) { 78 throw new IllegalStateException (ex.getMessage()); 79 } 80 } 81 82 protected byte[] engineDigest() { 83 byte[] digest = md5.digest(); 84 md5.update(opad); 85 return md5.digest(digest); 86 } 87 88 protected int engineDigest(byte[] buf, int offset, int len) { 89 byte[] digest = md5.digest(); 90 md5.update(opad); 91 md5.update(digest); 92 try { 93 return md5.digest(buf, offset, len); 94 } catch (Exception ex) { 95 throw new IllegalStateException (); 96 } 97 } 98 99 protected int engineGetDigestLength() { 100 return md5.getDigestLength(); 101 } 102 103 protected void engineReset() { 104 md5.reset(); 105 md5.update(ipad); 106 } 107 108 protected void engineUpdate(byte b) { 109 md5.update(b); 110 } 111 112 protected void engineUpdate(byte[] input, int offset, int len) { 113 md5.update(input, offset, len); 114 } 115 116 } 117 | Popular Tags |