00001 // $Id: XrdCryptosslMsgDigest.cc 30949 2009-11-02 16:37:58Z ganis $ 00002 00003 const char *XrdCryptosslMsgDigestCVSID = "$Id: XrdCryptosslMsgDigest.cc 30949 2009-11-02 16:37:58Z ganis $"; 00004 /******************************************************************************/ 00005 /* */ 00006 /* X r d C r y p t o M s g D i g e s t . c c */ 00007 /* */ 00008 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00009 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */ 00010 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00011 /* DE-AC03-76-SFO0515 with the Department of Energy */ 00012 /******************************************************************************/ 00013 00014 /* ************************************************************************** */ 00015 /* */ 00016 /* OpenSSL implementation of XrdCryptoMsgDigest */ 00017 /* */ 00018 /* ************************************************************************** */ 00019 00020 #include <XrdCrypto/XrdCryptoAux.hh> 00021 #include <XrdCrypto/XrdCryptosslTrace.hh> 00022 #include <XrdCrypto/XrdCryptosslMsgDigest.hh> 00023 00024 //_____________________________________________________________________________ 00025 XrdCryptosslMsgDigest::XrdCryptosslMsgDigest(const char *dgst) : 00026 XrdCryptoMsgDigest() 00027 { 00028 // Constructor. 00029 // Init the message digest calculation 00030 00031 valid = 0; 00032 SetType(0); 00033 Init(dgst); 00034 } 00035 00036 //_____________________________________________________________________________ 00037 bool XrdCryptosslMsgDigest::IsSupported(const char *dgst) 00038 { 00039 // Check if the specified MD is supported 00040 00041 return (EVP_get_digestbyname(dgst) != 0); 00042 } 00043 00044 //_____________________________________________________________________________ 00045 int XrdCryptosslMsgDigest::Init(const char *dgst) 00046 { 00047 // Initialize the buffer for the message digest calculation 00048 EPNAME("MsgDigest::Init"); 00049 00050 // Get message digest handle 00051 const EVP_MD *md = 0; 00052 // We first try the one input, if any 00053 if (dgst) 00054 md = EVP_get_digestbyname(dgst); 00055 00056 // If it did not work, we reuse the old one, or we use the default 00057 if (!md) { 00058 if (Type()) 00059 md = EVP_get_digestbyname(Type()); 00060 else 00061 md = EVP_get_digestbyname("sha1"); 00062 } 00063 if (!md) { 00064 DEBUG("cannot get msg digest by name"); 00065 return -1; 00066 } 00067 00068 // Init digest machine 00069 EVP_DigestInit(&mdctx, md); 00070 00071 // Successful initialization 00072 SetType(dgst); 00073 valid = 1; 00074 00075 // OK 00076 return 0; 00077 } 00078 00079 //_____________________________________________________________________________ 00080 int XrdCryptosslMsgDigest::Reset(const char *dgst) 00081 { 00082 // Re-Init the message digest calculation 00083 00084 valid = 0; 00085 Init(dgst); 00086 if (!valid) 00087 // unsuccessful initialization 00088 return -1; 00089 00090 return 0; 00091 } 00092 00093 //_____________________________________________________________________________ 00094 int XrdCryptosslMsgDigest::Update(const char *b, int l) 00095 { 00096 // Update message digest with the MD of l bytes at b. 00097 // Create the internal buffer if needed (first call) 00098 // Returns -1 if unsuccessful (digest not initialized), 0 otherwise. 00099 00100 if (Type()) { 00101 EVP_DigestUpdate(&mdctx, (char *)b, l); 00102 return 0; 00103 } 00104 return -1; 00105 } 00106 00107 //_____________________________________________________________________________ 00108 int XrdCryptosslMsgDigest::Final() 00109 { 00110 // Finalize message digest calculation. 00111 // Finalize the operation 00112 // Returns -1 if unsuccessful (digest not initialized), 0 otherwise. 00113 EPNAME("MsgDigest::Final"); 00114 00115 // MD outputs in these variables 00116 unsigned char mdval[EVP_MAX_MD_SIZE] = {0}; 00117 unsigned int mdlen = 0; 00118 00119 if (Type()) { 00120 // Finalize what we have 00121 EVP_DigestFinal(&mdctx, mdval, &mdlen); 00122 // Save result 00123 SetBuffer(mdlen,(const char *)mdval); 00124 // Notify, if requested 00125 DEBUG("result length is "<<mdlen << 00126 " bytes (hex: " << AsHexString() <<")"); 00127 return 0; 00128 } 00129 return -1; 00130 }