commands/hash/hash.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // hash.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/ioutils.h>
       
    14 #include <fshell/ltkutils.h>
       
    15 #include <fshell/common.mmh>
       
    16 #include <hash.h>
       
    17 
       
    18 const TInt KBlockSize = 512;
       
    19 
       
    20 using namespace IoUtils;
       
    21 
       
    22 class CCmdHash : public CCommandBase
       
    23 	{
       
    24 public:
       
    25 	static CCommandBase* NewLC();
       
    26 	~CCmdHash();
       
    27 private:
       
    28 	CCmdHash();
       
    29 	CMessageDigest::THashId HashIdL();
       
    30 private: // From CCommandBase.
       
    31 	virtual const TDesC& Name() const;
       
    32 	virtual void DoRunL();
       
    33 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    34 private:
       
    35 	TBool iVerbose;
       
    36 	TFileName2 iInputFileName;
       
    37 	TFileName2 iOutputFileName;
       
    38 	enum TAlgorithm
       
    39 		{
       
    40 		ESha1,
       
    41 		ESha224,
       
    42 		ESha256,
       
    43 		ESha384,
       
    44 		ESha512,
       
    45 		EMd2,
       
    46 		EMd4,
       
    47 		EMd5
       
    48 		} iAlgorithm;
       
    49 	CMessageDigest* iMessageDigest;
       
    50 	};
       
    51 
       
    52 
       
    53 CCommandBase* CCmdHash::NewLC()
       
    54 	{
       
    55 	CCmdHash* self = new(ELeave) CCmdHash();
       
    56 	CleanupStack::PushL(self);
       
    57 	self->BaseConstructL();
       
    58 	return self;
       
    59 	}
       
    60 
       
    61 CCmdHash::~CCmdHash()
       
    62 	{
       
    63 	delete iMessageDigest;
       
    64 	}
       
    65 
       
    66 CCmdHash::CCmdHash()
       
    67 	{
       
    68 	}
       
    69 
       
    70 CMessageDigest::THashId CCmdHash::HashIdL()
       
    71 	{
       
    72 	switch (iAlgorithm)
       
    73 		{
       
    74 		case ESha1:
       
    75 			return CMessageDigest::ESHA1;
       
    76 		case EMd2:
       
    77 			return CMessageDigest::EMD2;
       
    78 #ifdef FSHELL_MD4_SUPPORT
       
    79 		case EMd4:
       
    80 			return CMessageDigest::EMD4;
       
    81 #endif // FSHELL_MD4_SUPPORT
       
    82 		case EMd5:
       
    83 			return CMessageDigest::EMD5;
       
    84 #ifdef FSHELL_SHA2_SUPPORT
       
    85 		case ESha224:
       
    86 			return CMessageDigest::ESHA224;
       
    87 		case ESha256:
       
    88 			return CMessageDigest::ESHA256;
       
    89 		case ESha384:
       
    90 			return CMessageDigest::ESHA384;
       
    91 		case ESha512:
       
    92 			return CMessageDigest::ESHA512;
       
    93 #endif // FSHELL_SHA2_SUPPORT
       
    94 		default:
       
    95 			LeaveIfErr(KErrNotSupported, _L("Requested hashing algorithm is not available in this Symbian OS platform"));
       
    96 			return CMessageDigest::ESHA1;
       
    97 		}
       
    98 	}
       
    99 
       
   100 const TDesC& CCmdHash::Name() const
       
   101 	{
       
   102 	_LIT(KName, "hash");	
       
   103 	return KName;
       
   104 	}
       
   105 
       
   106 void CCmdHash::DoRunL()
       
   107 	{
       
   108 	iMessageDigest = CMessageDigestFactory::NewDigestL(HashIdL());
       
   109 
       
   110 	RIoReadHandle readHandle;
       
   111 	readHandle.Create(IoSession());
       
   112 	CleanupClosePushL(readHandle);
       
   113 
       
   114 	RIoFile ioFile;
       
   115 	if (iOptions.IsPresent(&iInputFileName))
       
   116 		{
       
   117 		LeaveIfErr(ioFile.Create(IoSession(), iInputFileName, RIoFile::ERead), _L("Couldn't open handle to file '%S'"), &iInputFileName);
       
   118 		CleanupClosePushL(ioFile);
       
   119 		LeaveIfErr(ioFile.Attach(readHandle, RIoEndPoint::EForeground), _L("Couldn't attach read handle to file object"));
       
   120 		}
       
   121 	else
       
   122 		{
       
   123 		LeaveIfErr(readHandle.Duplicate(Stdin()), _L("Couldn't duplicate STDIN"));
       
   124 		}
       
   125 
       
   126 	LeaveIfErr(readHandle.SetReadMode(RIoReadHandle::EOneOrMore), _L("Couldn't set read handle into read one or more mode"));
       
   127 
       
   128 	TBuf<KBlockSize> buf;
       
   129 	TInt err = KErrNone;
       
   130 	do
       
   131 		{
       
   132 		err = readHandle.Read(buf);
       
   133 		if (err == KErrNone)
       
   134 			{
       
   135 			iMessageDigest->Update(buf.Collapse());
       
   136 			}
       
   137 		}
       
   138 		while (err == KErrNone);
       
   139 
       
   140 	if (err != KErrEof)
       
   141 		{
       
   142 		LeaveIfErr(err, _L("Unable to read input"));
       
   143 		}
       
   144 
       
   145 	TPtrC8 hash(iMessageDigest->Final());
       
   146 
       
   147 	if (iOptions.IsPresent(&iOutputFileName))
       
   148 		{
       
   149 		RFile outputFile;
       
   150 		LeaveIfErr(outputFile.Create(FsL(), iOutputFileName, EFileWrite), _L("Couldn't create '%S'"), &iOutputFileName);
       
   151 		CleanupClosePushL(outputFile);
       
   152 		outputFile.Write(hash);
       
   153 		CleanupStack::PopAndDestroy(&outputFile);
       
   154 		}
       
   155 	else
       
   156 		{
       
   157 		LtkUtils::HexDumpToOutput(hash, Stdout());
       
   158 		}
       
   159 
       
   160 	if (iOptions.IsPresent(&iInputFileName))
       
   161 		{
       
   162 		CleanupStack::PopAndDestroy(&ioFile);
       
   163 		}
       
   164 
       
   165 	CleanupStack::PopAndDestroy(&readHandle);
       
   166 	}
       
   167 
       
   168 void CCmdHash::OptionsL(RCommandOptionList& aOptions)
       
   169 	{
       
   170 	aOptions.AppendBoolL(iVerbose, _L("verbose"));
       
   171 	aOptions.AppendFileNameL(iInputFileName, _L("input"));
       
   172 	aOptions.AppendFileNameL(iOutputFileName, _L("output"));
       
   173 	aOptions.AppendEnumL((TInt&)iAlgorithm, _L("algorithm"));
       
   174 	}
       
   175 
       
   176 
       
   177 EXE_BOILER_PLATE(CCmdHash)
       
   178