home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / utils / hmacSigner.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  2.3 KB  |  125 lines

  1. package org.xbill.DNS.utils;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5.  
  6. public class hmacSigner {
  7.    private byte[] ipad;
  8.    private byte[] opad;
  9.    private ByteArrayOutputStream bytes;
  10.    private static final byte IPAD = 54;
  11.    private static final byte OPAD = 92;
  12.    private static final byte PADLEN = 64;
  13.    public static boolean verbose = false;
  14.  
  15.    public hmacSigner(byte[] key) {
  16.       if (key.length > 64) {
  17.          key = md5.compute(key);
  18.       }
  19.  
  20.       this.ipad = new byte[64];
  21.       this.opad = new byte[64];
  22.  
  23.       int i;
  24.       for(i = 0; i < key.length; ++i) {
  25.          this.ipad[i] = (byte)(key[i] ^ 54);
  26.          this.opad[i] = (byte)(key[i] ^ 92);
  27.       }
  28.  
  29.       while(i < 64) {
  30.          this.ipad[i] = 54;
  31.          this.opad[i] = 92;
  32.          ++i;
  33.       }
  34.  
  35.       this.bytes = new ByteArrayOutputStream();
  36.  
  37.       try {
  38.          this.bytes.write(this.ipad);
  39.       } catch (IOException var4) {
  40.       }
  41.  
  42.       if (verbose) {
  43.          System.err.println(hexdump.dump("key", key));
  44.       }
  45.  
  46.    }
  47.  
  48.    public void addData(byte[] b, int offset, int length) {
  49.       if (length >= 0 && offset + length <= b.length) {
  50.          if (verbose) {
  51.             System.err.println(hexdump.dump("partial add", b, offset, length));
  52.          }
  53.  
  54.          this.bytes.write(b, offset, length);
  55.       } else {
  56.          if (verbose) {
  57.             System.err.println("Invalid parameters");
  58.          }
  59.  
  60.       }
  61.    }
  62.  
  63.    public void addData(byte[] b) {
  64.       if (verbose) {
  65.          System.err.println(hexdump.dump("add", b));
  66.       }
  67.  
  68.       try {
  69.          this.bytes.write(b);
  70.       } catch (IOException var3) {
  71.       }
  72.  
  73.    }
  74.  
  75.    public byte[] sign() {
  76.       byte[] output = md5.compute(this.bytes.toByteArray());
  77.       this.bytes = new ByteArrayOutputStream();
  78.  
  79.       try {
  80.          this.bytes.write(this.opad);
  81.          this.bytes.write(output);
  82.       } catch (IOException var3) {
  83.       }
  84.  
  85.       byte[] b = md5.compute(this.bytes.toByteArray());
  86.       if (verbose) {
  87.          System.err.println(hexdump.dump("sig", b));
  88.       }
  89.  
  90.       return b;
  91.    }
  92.  
  93.    public boolean verify(byte[] signature) {
  94.       if (verbose) {
  95.          System.err.println(hexdump.dump("ver", signature));
  96.       }
  97.  
  98.       return byteArrayCompare(signature, this.sign());
  99.    }
  100.  
  101.    public void clear() {
  102.       this.bytes = new ByteArrayOutputStream();
  103.  
  104.       try {
  105.          this.bytes.write(this.ipad);
  106.       } catch (IOException var2) {
  107.       }
  108.  
  109.    }
  110.  
  111.    private static boolean byteArrayCompare(byte[] b1, byte[] b2) {
  112.       if (b1.length != b2.length) {
  113.          return false;
  114.       } else {
  115.          for(int i = 0; i < b1.length; ++i) {
  116.             if (b1[i] != b2[i]) {
  117.                return false;
  118.             }
  119.          }
  120.  
  121.          return true;
  122.       }
  123.    }
  124. }
  125.