Bug 722034 - Part 2 - Factor out nsHttpChannel::Hash into a standalone function, r=biesi

This commit is contained in:
Brian Smith 2012-04-05 01:57:30 -07:00
parent f7170ae8c4
commit c0309d87b7
2 changed files with 25 additions and 34 deletions

View File

@ -10,6 +10,7 @@
#include "nsIApplicationCacheService.h"
#include "nsIApplicationCacheContainer.h"
#include "nsIAuthInformation.h"
#include "nsICryptoHash.h"
#include "nsIStringBundle.h"
#include "nsIIDNService.h"
#include "nsIStreamListenerTee.h"
@ -50,6 +51,30 @@ const char kOfflineDeviceID[] = "offline";
static NS_DEFINE_CID(kStreamListenerTeeCID, NS_STREAMLISTENERTEE_CID);
// Computes and returns a SHA1 hash of the input buffer. The input buffer
// must be a null-terminated string.
nsresult
Hash(const char *buf, nsACString &hash)
{
nsresult rv;
nsCOMPtr<nsICryptoHash> hasher
= do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = hasher->Init(nsICryptoHash::SHA1);
NS_ENSURE_SUCCESS(rv, rv);
rv = hasher->Update(reinterpret_cast<unsigned const char*>(buf),
strlen(buf));
NS_ENSURE_SUCCESS(rv, rv);
rv = hasher->Finish(true, hash);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
bool IsRedirectStatus(PRUint32 status)
{
// 305 disabled as a security measure (see bug 187996).
@ -1741,31 +1766,6 @@ nsHttpChannel::HandleAsyncAbort()
}
nsresult
nsHttpChannel::Hash(const char *buf, nsACString &hash)
{
nsresult rv;
if (!mHasher) {
mHasher = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
LOG(("nsHttpChannel: Failed to instantiate crypto-hasher"));
return rv;
}
}
rv = mHasher->Init(nsICryptoHash::SHA1);
NS_ENSURE_SUCCESS(rv, rv);
rv = mHasher->Update(reinterpret_cast<unsigned const char*>(buf),
strlen(buf));
NS_ENSURE_SUCCESS(rv, rv);
rv = mHasher->Finish(true, hash);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult
nsHttpChannel::EnsureAssocReq()
{

View File

@ -26,7 +26,6 @@
#include "nsIHttpAuthenticableChannel.h"
#include "nsIHttpChannelAuthProvider.h"
#include "nsIAsyncVerifyRedirectCallback.h"
#include "nsICryptoHash.h"
#include "nsITimedChannel.h"
#include "nsDNSPrefetch.h"
#include "TimingStruct.h"
@ -261,12 +260,6 @@ private:
*/
nsresult ProcessSTSHeader();
/**
* Computes and returns a 64 bit encoded string holding a hash of the
* input buffer. Input buffer must be a null-terminated string.
*/
nsresult Hash(const char *buf, nsACString &hash);
void InvalidateCacheEntryForLocation(const char *location);
void AssembleCacheKey(const char *spec, PRUint32 postID, nsACString &key);
nsresult CreateNewURI(const char *loc, nsIURI **newURI);
@ -349,8 +342,6 @@ private:
nsTArray<nsContinueRedirectionFunc> mRedirectFuncStack;
nsCOMPtr<nsICryptoHash> mHasher;
PRTime mChannelCreationTime;
mozilla::TimeStamp mChannelCreationTimestamp;
mozilla::TimeStamp mAsyncOpenTime;