1 19 20 package com.maverick.crypto.digests; 21 22 23 24 25 30 public class GeneralHMac 31 implements HMac { 32 private final static int BLOCK_LENGTH = 64; 33 34 private final static byte IPAD = (byte) 0x36; 35 private final static byte OPAD = (byte) 0x5C; 36 37 private Digest digest; 38 private int digestSize; 39 private int outputSize; 40 private byte[] inputPad = new byte[BLOCK_LENGTH]; 41 private byte[] outputPad = new byte[BLOCK_LENGTH]; 42 43 public GeneralHMac( 44 Digest digest) { 45 this.digest = digest; 46 digestSize = digest.getDigestSize(); 47 outputSize = digestSize; 48 } 49 50 public GeneralHMac( 51 Digest digest, int outputSize) { 52 this.digest = digest; 53 this.digestSize = digest.getDigestSize(); 54 this.outputSize = outputSize; 55 } 56 57 public String getAlgorithmName() { 58 return digest.getAlgorithmName() + "/HMAC"; 59 } 60 61 public int getOutputSize() { 62 return outputSize; 63 } 64 65 public Digest getUnderlyingDigest() { 66 return digest; 67 } 68 69 public void init( 70 byte[] key) { 71 digest.reset(); 72 73 if (key.length > BLOCK_LENGTH) { 74 digest.update(key, 0, key.length); 75 digest.doFinal(inputPad, 0); 76 for (int i = digestSize; i < inputPad.length; i++) { 77 inputPad[i] = 0; 78 } 79 } 80 else { 81 System.arraycopy(key, 0, inputPad, 0, key.length); 82 for (int i = key.length; i < inputPad.length; i++) { 83 inputPad[i] = 0; 84 } 85 } 86 87 outputPad = new byte[inputPad.length]; 88 System.arraycopy(inputPad, 0, outputPad, 0, inputPad.length); 89 90 for (int i = 0; i < inputPad.length; i++) { 91 inputPad[i] ^= IPAD; 92 } 93 94 for (int i = 0; i < outputPad.length; i++) { 95 outputPad[i] ^= OPAD; 96 } 97 98 digest.update(inputPad, 0, inputPad.length); 99 } 100 101 public int getMacSize() { 102 return digestSize; 103 } 104 105 public void update( 106 byte in) { 107 digest.update(in); 108 } 109 110 public void update( 111 byte[] in, 112 int inOff, 113 int len) { 114 digest.update(in, inOff, len); 115 } 116 117 public int doFinal( 118 byte[] out, 119 int outOff) { 120 byte[] tmp = new byte[digestSize]; 121 digest.doFinal(tmp, 0); 122 123 digest.update(outputPad, 0, outputPad.length); 124 digest.update(tmp, 0, tmp.length); 125 126 int len = digest.doFinal(out, outOff); 127 128 reset(); 129 130 return len; 131 } 132 133 136 public void reset() { 137 140 digest.reset(); 141 142 145 digest.update(inputPad, 0, inputPad.length); 146 } 147 } 148 | Popular Tags |