home *** CD-ROM | disk | FTP | other *** search
- package org.xbill.DNS.utils;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
-
- public class hmacSigner {
- private byte[] ipad;
- private byte[] opad;
- private ByteArrayOutputStream bytes;
- private static final byte IPAD = 54;
- private static final byte OPAD = 92;
- private static final byte PADLEN = 64;
- public static boolean verbose = false;
-
- public hmacSigner(byte[] key) {
- if (key.length > 64) {
- key = md5.compute(key);
- }
-
- this.ipad = new byte[64];
- this.opad = new byte[64];
-
- int i;
- for(i = 0; i < key.length; ++i) {
- this.ipad[i] = (byte)(key[i] ^ 54);
- this.opad[i] = (byte)(key[i] ^ 92);
- }
-
- while(i < 64) {
- this.ipad[i] = 54;
- this.opad[i] = 92;
- ++i;
- }
-
- this.bytes = new ByteArrayOutputStream();
-
- try {
- this.bytes.write(this.ipad);
- } catch (IOException var4) {
- }
-
- if (verbose) {
- System.err.println(hexdump.dump("key", key));
- }
-
- }
-
- public void addData(byte[] b, int offset, int length) {
- if (length >= 0 && offset + length <= b.length) {
- if (verbose) {
- System.err.println(hexdump.dump("partial add", b, offset, length));
- }
-
- this.bytes.write(b, offset, length);
- } else {
- if (verbose) {
- System.err.println("Invalid parameters");
- }
-
- }
- }
-
- public void addData(byte[] b) {
- if (verbose) {
- System.err.println(hexdump.dump("add", b));
- }
-
- try {
- this.bytes.write(b);
- } catch (IOException var3) {
- }
-
- }
-
- public byte[] sign() {
- byte[] output = md5.compute(this.bytes.toByteArray());
- this.bytes = new ByteArrayOutputStream();
-
- try {
- this.bytes.write(this.opad);
- this.bytes.write(output);
- } catch (IOException var3) {
- }
-
- byte[] b = md5.compute(this.bytes.toByteArray());
- if (verbose) {
- System.err.println(hexdump.dump("sig", b));
- }
-
- return b;
- }
-
- public boolean verify(byte[] signature) {
- if (verbose) {
- System.err.println(hexdump.dump("ver", signature));
- }
-
- return byteArrayCompare(signature, this.sign());
- }
-
- public void clear() {
- this.bytes = new ByteArrayOutputStream();
-
- try {
- this.bytes.write(this.ipad);
- } catch (IOException var2) {
- }
-
- }
-
- private static boolean byteArrayCompare(byte[] b1, byte[] b2) {
- if (b1.length != b2.length) {
- return false;
- } else {
- for(int i = 0; i < b1.length; ++i) {
- if (b1[i] != b2[i]) {
- return false;
- }
- }
-
- return true;
- }
- }
- }
-