1   /* $RCSfile: RIPEMD160ByteCounter.java,v $
2    * $Revision: 1.9 $
3    * $Date: 2002/01/04 13:02:31 $
4    * $Author: uwe $
5    * $State: Exp $
6    *
7    * Created on July 15, 2001, 6:24 PM
8    *
9    * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
10   *
11   * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
12   * ServiceProvider is a library, written in JavaTM, that should be 
13   * used in HBCI banking applications (clients and may be servers),
14   * to do cryptographic operations.
15   *
16   * The jhbci library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * The jhbci library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29   *
30   */
31  
32  package de.cscc.crypto.provider;
33  
34  import de.cscc.crypto.util.LongUtil;
35  
36  /** 
37   * Used to count the bytes and computes the bits of the whole message that are 
38   * processed by RIPEMD160Digest.
39   *
40   * @author  <a href=mailto:uwe@cscc.de>Uwe G&uuml;nther</a>
41   * @version $Revision: 1.9 $
42   */
43  class RIPEMD160ByteCounter implements Cloneable {
44  
45      /** Internal counter representation of the object. */
46      private long byteCounter;
47      
48      /** finalBitLength will be set through the setFinalBitLength()*/
49      private long finalBitLength;
50      
51      /** Creates new RIPEMD160ByteCounter. */
52      public RIPEMD160ByteCounter() {}
53  
54      /** 
55       * Creates and returns a deep copy of this object.
56       * @return a clone of this instance.
57       * @see java.lang.Cloneable
58       * @exception CloneNotSupportedException if the object's class does not
59       *             support the <code>Cloneable</code> interface. Subclasses
60       *             that override the <code>clone</code> method can also
61       *             throw this exception to indicate that an instance cannot
62       *             be cloned.
63       */
64      public Object clone() throws CloneNotSupportedException {
65          return super.clone();
66      }        
67      
68      /** 
69       * Indicates whether some other object is "equal to" this one.
70       *
71       * @param   obj   the reference object with which to compare.
72       * @return  <code>true</code> if this object is the same as the obj
73       *         argument; <code>false</code> otherwise.
74       * @see     #hashCode()
75       * @see     java.util.Hashtable
76       */
77      public boolean equals(Object obj) {
78          //Only for performance.
79          if (this == obj) {
80              return true;
81          } 
82          
83          //If obj == null then instanceof returns false, see JLS 15.20.2
84          if (!(obj instanceof RIPEMD160ByteCounter)) {                      
85              return false;
86          }
87          
88          RIPEMD160ByteCounter temp = (RIPEMD160ByteCounter)obj;
89          return (this.byteCounter    == temp.byteCounter    &&
90                  this.finalBitLength == temp.finalBitLength );
91      }
92      
93      /** 
94       * Returns a hash code value for the object. This method is
95       * supported for the benefit of hashtables such as those provided by
96       * <code>java.util.Hashtable</code>.
97       *
98       * @return  a hash code value for this object.
99       * @see     #equals(java.lang.Object)
100      * @see     java.util.Hashtable
101      */
102     public int hashCode() {
103         int result = 17;
104         result = 37*result + (int) (this.byteCounter ^ 
105                                    (this.byteCounter >>> 32));
106         result = 37*result + (int) (this.finalBitLength ^ 
107                                    (this.finalBitLength >>> 32));
108         return result;
109     }    
110     
111     /**
112      * Returns a string representation of the object. 
113      *
114      * @return  a string representation of the object.
115      */
116     public String toString() {
117         return "[byteCounter: " + LongUtil.toHex(this.byteCounter) + 
118                " finalBitLength: " + LongUtil.toHex(this.finalBitLength) + "]";
119     }
120     
121     /** Increments the value of RIPEMD160ByteCounter. */
122     public void inc() {
123         this.byteCounter++;
124     }
125     
126     /** Resets the internal values to the same settings as after construction. */
127     public void reset() {
128         this.byteCounter = 0;
129         this.finalBitLength = 0;
130     }
131     
132     /** Saves the byteCounter value that is valid at this time now in the 
133      * as bit value (2^3 or byteCounter << 3) object. 
134      */
135     public void setFinalBitLength() {
136         this.finalBitLength = this.byteCounter << 3;
137     }
138     
139     /** 
140      * Get the internal finalBitLength that you have saved before with
141      * {@link #setFinalBitLength() long getFinalBitLength()}
142      *
143      * @return the final bit length of the whole message that are processed.
144      */
145     public long getFinalBitLength() {
146         return this.finalBitLength;
147     }
148 }
149