Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DigestClientAlgorithm.java
   *
   * Created on January 7, 2003, 10:45 AM
   */
  
  package examples.authorization;
  
  
Get this interface from the nist-sip IM

Author(s):
olivier deruelle
 
 
     private String realm;
     private String userName;
     private String uri;
     private String nonce;
     private String password;
     private String method;
     private String cnonce;
     private MessageDigest messageDigest;


     
to hex converter
 
     private static final char[] toHex = { '0''1''2''3''4''5''6',
     '7''8''9''a''b''c''d''e''f' };

    
convert an array of bytes to an hexadecimal string

Parameters:
b bytes array to convert to a hexadecimal string
Returns:
a string
 
 
     public static String toHexString(byte b[]) {
         int pos = 0;
         char[] c = new char[b.length*2];
         for (int i=0; ib.lengthi++) {
             c[pos++] = [(b[i] >> 4) & 0x0F];
             c[pos++] = [b[i] & 0x0f];
         }
         return new String(c);
     }
 
     public void initialize(String realmString userNameString uriString nonce,
     String passwordString methodString cnonce,String algorithmthrows Exception {
         if (realm==nullthrow new Exception("The realm parameter is null");
             this.=realm;
         if (userName==nullthrow new Exception("The userName parameter is null");
             this.=userName;
         if (uri==nullthrow new Exception("The uri parameter is null");
             this.=uri;
         if (nonce==nullthrow new Exception("The nonce parameter is null");
             this.=nonce;
         if (password==nullthrow new Exception("The password parameter is null");
             this.=password;
         if (method==nullthrow new Exception("The method parameter is null");
             this.=method;
         this.=cnonce;
         if (algorithm==nullthrow new Exception("The algorithm parameter is null");
             try {
              = MessageDigest.getInstance(algorithm);
         }
         catch ( NoSuchAlgorithmException ex ) {
         ..println("DEBUG, DigestClientAuthenticationMethod, initialize(): "+
             "ERROR: Digest algorithm does not exist.");
             throw new Exception("ERROR: Digest algorithm does not exist.");
     }
     }

     
generate the response
 
     public String generateResponse() {
      if ( == null) {
             ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
             "ERROR: no userName parameter");
             return null;
          }
      if ( == null) {
             ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
             "ERROR: no realm parameter");
             return  null;
          }
 
          ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
          "Trying to generate a response for the user: "++" , with "+
          "the realm: ");
 
      if ( == null)  {
             ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
             "ERROR: no password parameter");
         return null;
         }
         if ( == null)  {
            ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
            "ERROR: no method parameter");
        return null;
         }
         if (== null)  {
            ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
            "ERROR: no uri parameter");
        return null;
         }
         if (== null)  {
            ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
            "ERROR: no nonce parameter");
        return null;
         }
         if (== null)  {
            ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "+
            "ERROR: the algorithm is not set");
        return null;
         }


         
GENERATE RESPONSE **********************************
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), userName:"++"!");
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), realm:"++"!");
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), password:"++"!");
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), uri:"++"!");
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), nonce:"++"!");
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), method:"++"!");
         // A1
         String A1 =  + ":" + ":" +    ;
         byte mdbytes[] = .digest(A1.getBytes());
         String HA1 = toHexString(mdbytes);
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), HA1:"+HA1+"!");
         //A2
         String A2 = .toUpperCase() + ":" +  ;
         mdbytes = .digest(A2.getBytes());
         String HA2 = toHexString(mdbytes);
         ..println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), HA2:"+HA2+"!");
         //KD
         String KD = HA1 + ":" + ;
         if ( != null) {
             if(.length()>0) KD += ":" + ;
         }
         KD += ":" + HA2;
         mdbytes = .digest(KD.getBytes());
         String response = toHexString(mdbytes);
         ..println("DEBUG, DigestClientAlgorithm, generateResponse():"+
         " response generated: "+response);
         return response;
    }
New to GrepCode? Check out our FAQ X