webservices/wsutils/src/sencryptoutils.cpp
changeset 23 a1df79fa35b4
parent 0 62f9d29f7211
equal deleted inserted replaced
16:abb636afedf7 23:a1df79fa35b4
    20 
    20 
    21 
    21 
    22 
    22 
    23 
    23 
    24 
    24 
    25 
    25 #include <e32math.h>
    26 
    26 #include <tconvbase64.h>
    27 #include "sencryptoutils.h"
    27 #include "sencryptoutils.h"
    28 #include <imcvcodc.h>
       
    29 #include "senguidgen.h"
    28 #include "senguidgen.h"
    30 #include <e32math.h>
    29 #include "SenDomFragment.h"
    31 #include <SenDomFragment.h>
    30 #include "SenNameSpace.h"
    32 #include <SenNameSpace.h>
       
    33 #include "SenWsSecurityHeader.h"
    31 #include "SenWsSecurityHeader.h"
    34 //#include "senxmldebug.h"
       
    35 
    32 
    36 namespace
    33 namespace
    37     {
    34     {
    38     _LIT(KTimeReference,         "19700000:000000");
    35     _LIT(KTimeReference,         "19700000:000000");
    39     const TInt KToBase64CoefficientNumerator  = 4;
    36     const TInt KToBase64CoefficientNumerator  = 4;
    88 {
    85 {
    89     if (aData.Length() == 0)
    86     if (aData.Length() == 0)
    90         {
    87         {
    91         return NULL;
    88         return NULL;
    92         }
    89         }
    93 
    90     TInt length = aData.Length(); 
    94     TInt remainder = aData.Length() % KToBase64CoefficientNominative;
    91     TInt destLen = (length + 1 ) * 2;
    95     TInt length = aData.Length() * KToBase64CoefficientNumerator;
    92     HBufC8* buffer = HBufC8::NewMaxL(destLen);
    96     length /= KToBase64CoefficientNominative;
    93     TInt test = buffer->Des().Length(); 
    97     if (remainder)
    94     TPtr8 encodedPtr(buffer->Des());
    98         {
    95     TBase64 base64Codec;
    99         length++;
    96     TInt error = base64Codec.Encode(aData, encodedPtr);
   100         length += KToBase64CoefficientNominative - remainder;
    97 	if(error > 1) //There is a problem in encoding always returns 1
   101         }
    98 		{
   102 
    99 		delete buffer;
   103     HBufC8* buffer = HBufC8::NewLC(length);
   100 		buffer = NULL;		
   104     
   101 		}
   105     
   102 	else
   106     TImCodecB64 base64Codec;
   103 		{
   107     base64Codec.Initialise();
   104 		//Successful encoding
   108     TPtr8 des = buffer->Des();  // don't blame me, it's the compiler's fault    
   105 		}
   109     base64Codec.Encode(aData, des);
       
   110     
       
   111     CleanupStack::Pop(buffer);
       
   112     return buffer;
   106     return buffer;
   113 }
   107 }
   114 
   108 
   115 EXPORT_C HBufC8* SenCryptoUtils::DecodeBase64L(const TDesC8& aData)
   109 EXPORT_C HBufC8* SenCryptoUtils::DecodeBase64L(const TDesC8& aData)
   116 {
   110 {
   117     const TUint8 KBase64Fill = '=';
   111 	TInt data_len = aData.Length();
   118 
   112 	HBufC8* buffer = NULL;
   119     TInt length = aData.Length();
   113 	if (data_len > 0)
   120     if (aData[length-1] == KBase64Fill)
   114 		{
   121         {
   115 		buffer = aData.AllocL();
   122         length--;
   116 		buffer->Des().Zero(); //added
   123         if (aData[length-2] == KBase64Fill)
   117 		TPtr8 des( buffer->Des() );
   124             {
   118 		TBase64 base64Codec;
   125             length--;
   119 //		if (base64Codec.Decode(aData,des) != EFalse)
   126             }
   120 //			{
   127         }
   121 //			//Successful decoding
   128     length *= KToBase64CoefficientNominative;
   122 //			}
   129     length /= KToBase64CoefficientNumerator;
   123 //		else
   130     if (length <= 0)
   124 //			{
   131         {
   125 //			delete buffer;			
   132         return NULL;
   126 //			buffer = NULL;
   133         }
   127 //			}
   134 
   128 		base64Codec.Decode(aData, des);
   135     HBufC8* buffer = HBufC8::NewLC(length);
   129 		}
   136 
       
   137     TImCodecB64 base64Codec;
       
   138     base64Codec.Initialise();
       
   139     TPtr8 des = buffer->Des();  // don't blame me, it's the compiler's fault    
       
   140 
       
   141     base64Codec.Decode(aData, des);
       
   142 
       
   143     CleanupStack::Pop(buffer);
       
   144     return buffer;    
   130     return buffer;    
   145 }
   131 }
   146 
   132 
   147 EXPORT_C HBufC8* SenCryptoUtils::RandomAndHashMd5LC()
   133 EXPORT_C HBufC8* SenCryptoUtils::RandomAndHashMd5LC()
   148     {
   134     {