001         package com.croftsoft.core.util.cache.secure;
002    
003         import java.security.MessageDigest;
004    
005         import com.croftsoft.core.util.id.Id;
006    
007         /*********************************************************************
008         * A concrete Id implementation that uses a secure digest to verify
009         * identity.
010         *
011         * <P>
012         *
013         * @see
014         *   com.croftsoft.core.j1d1.util.id.Id;
015         * @see
016         *   java.security.MessageDigest
017         *
018         * @version
019         *   2000-08-19
020         * @author
021         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
022         *********************************************************************/
023    
024         public final class  SecureId implements Id
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028    
029         private final String    algorithm;
030    
031         private final byte [ ]  digest;
032    
033         //////////////////////////////////////////////////////////////////////
034         // Constructor methods
035         //////////////////////////////////////////////////////////////////////
036    
037         public  SecureId (
038           String    algorithm,
039           byte [ ]  digest )
040         //////////////////////////////////////////////////////////////////////
041         {
042           if ( algorithm == null )
043           {
044             throw new IllegalArgumentException ( "null algorithm" );
045           }
046    
047           if ( digest == null )
048           {
049             throw new IllegalArgumentException ( "null digest" );
050           }
051    
052           this.algorithm = algorithm;
053    
054           this.digest    = digest;
055         }
056    
057         //////////////////////////////////////////////////////////////////////
058         //////////////////////////////////////////////////////////////////////
059    
060         public String    getAlgorithm ( ) { return algorithm; }
061    
062         public byte [ ]  getDigest    ( ) { return digest;    }
063    
064         //////////////////////////////////////////////////////////////////////
065         //////////////////////////////////////////////////////////////////////
066    
067         public boolean  equals ( Object  other )
068         //////////////////////////////////////////////////////////////////////
069         {
070           if ( other == null ) return false;
071    
072           if ( !getClass ( ).equals ( other.getClass ( ) ) ) return false;
073    
074           SecureId  that = ( SecureId ) other;
075    
076           return MessageDigest.isEqual ( this.digest, that.digest )
077             && this.algorithm.equals ( that.algorithm );
078         }
079    
080         public int  hashCode ( )
081         //////////////////////////////////////////////////////////////////////
082         {
083           return new String ( digest ).hashCode ( );
084         }
085    
086         public Object  clone ( )
087         //////////////////////////////////////////////////////////////////////
088         {
089           try
090           {
091             return super.clone ( );
092           }
093           catch ( CloneNotSupportedException  ex )
094           {
095             // this will never happen
096    
097             throw new RuntimeException ( );
098           }
099         }
100    
101         //////////////////////////////////////////////////////////////////////
102         //////////////////////////////////////////////////////////////////////
103         }