Changeset 182895 in webkit


Ignore:
Timestamp:
Apr 16, 2015, 10:44:13 AM (11 years ago)
Author:
Antti Koivisto
Message:

Use CommonCrypto for SHA1 and MD5
https://bugs.webkit.org/show_bug.cgi?id=143826

Reviewed by Anders Carlsson.

CommonCrypto SHA1 implementation is ~4x faster than the naive WTF one. Use it when available.

These are covered by existing API tests.

  • wtf/MD5.cpp:

(WTF::MD5::MD5):
(WTF::MD5::addBytes):
(WTF::MD5::checksum):

  • wtf/MD5.h:
  • wtf/SHA1.cpp:

(WTF::SHA1::SHA1):
(WTF::SHA1::addBytes):
(WTF::SHA1::computeHash):

Remove the side effect where computeHash resets the state. No one relies on it.

(WTF::SHA1::hexDigest):
(WTF::SHA1::computeHexDigest):

  • wtf/SHA1.h:
Location:
trunk/Source/WTF
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r182890 r182895  
     12015-04-16  Antti Koivisto  <antti@apple.com>
     2
     3        Use CommonCrypto for SHA1 and MD5
     4        https://bugs.webkit.org/show_bug.cgi?id=143826
     5
     6        Reviewed by Anders Carlsson.
     7
     8        CommonCrypto SHA1 implementation is ~4x faster than the naive WTF one. Use it when available.
     9
     10        These are covered by existing API tests.
     11
     12        * wtf/MD5.cpp:
     13        (WTF::MD5::MD5):
     14        (WTF::MD5::addBytes):
     15        (WTF::MD5::checksum):
     16        * wtf/MD5.h:
     17        * wtf/SHA1.cpp:
     18        (WTF::SHA1::SHA1):
     19        (WTF::SHA1::addBytes):
     20        (WTF::SHA1::computeHash):
     21
     22            Remove the side effect where computeHash resets the state. No one relies on it.
     23
     24        (WTF::SHA1::hexDigest):
     25        (WTF::SHA1::computeHexDigest):
     26        * wtf/SHA1.h:
     27
    1282015-04-16  Csaba Osztrogonác  <ossy@webkit.org>
    229
  • trunk/Source/WTF/wtf/MD5.cpp

    r160460 r182895  
    33/*
    44 * Copyright (C) 2010 Google Inc. All rights reserved.
     5 * Copyright (C) 2015 Apple Inc. All rights reserved.
    56 *
    67 * Redistribution and use in source and binary forms, with or without
     
    5960namespace WTF {
    6061
     62#if PLATFORM(COCOA)
     63
     64MD5::MD5()
     65{
     66    CC_MD5_Init(&m_context);
     67}
     68
     69void MD5::addBytes(const uint8_t* input, size_t length)
     70{
     71    CC_MD5_Update(&m_context, input, length);
     72}
     73
     74void MD5::checksum(Digest& hash)
     75{
     76    CC_MD5_Final(hash.data(), &m_context);
     77}
     78   
     79#else
     80
    6181// Note: this code is harmless on little-endian machines.
    6282
     
    265285}
    266286
     287#endif
     288
    267289} // namespace WTF
  • trunk/Source/WTF/wtf/MD5.h

    r160386 r182895  
    3535#include <wtf/Vector.h>
    3636
     37#if PLATFORM(COCOA)
     38#include <CommonCrypto/CommonDigest.h>
     39#endif
     40
    3741namespace WTF {
    3842
     
    5761
    5862private:
     63#if PLATFORM(COCOA)
     64    CC_MD5_CTX m_context;
     65#else
    5966    uint32_t m_buf[4];
    6067    uint32_t m_bits[2];
    6168    uint8_t m_in[64];
     69#endif
    6270};
    6371
  • trunk/Source/WTF/wtf/SHA1.cpp

    r160456 r182895  
    11/*
    22 * Copyright (C) 2011 Google Inc. All rights reserved.
     3 * Copyright (C) 2015 Apple Inc. All rights reserved.
    34 *
    45 * Redistribution and use in source and binary forms, with or without
     
    2930 */
    3031
     32#include "config.h"
     33#include "SHA1.h"
     34
     35#include "Assertions.h"
     36
     37#include "StringExtras.h"
     38#include "text/CString.h"
     39
     40namespace WTF {
     41
     42#if PLATFORM(COCOA)
     43
     44SHA1::SHA1()
     45{
     46    CC_SHA1_Init(&m_context);
     47}
     48
     49void SHA1::addBytes(const uint8_t* input, size_t length)
     50{
     51    CC_SHA1_Update(&m_context, input, length);
     52}
     53
     54void SHA1::computeHash(Digest& hash)
     55{
     56    CC_SHA1_Final(hash.data(), &m_context);
     57}
     58
     59#else
     60
    3161// A straightforward SHA-1 implementation based on RFC 3174.
    3262// http://www.ietf.org/rfc/rfc3174.txt
    3363// The names of functions and variables (such as "a", "b", and "f") follow notations in RFC 3174.
    34 
    35 #include "config.h"
    36 #include "SHA1.h"
    37 
    38 #include "Assertions.h"
    39 
    40 #include "StringExtras.h"
    41 #include "text/CString.h"
    42 
    43 namespace WTF {
    4464
    4565static inline uint32_t f(int t, uint32_t b, uint32_t c, uint32_t d)
     
    105125}
    106126
    107 CString SHA1::hexDigest(const Digest& digest)
    108 {
    109     char* start = 0;
    110     CString result = CString::newUninitialized(40, start);
    111     char* buffer = start;
    112     for (size_t i = 0; i < hashSize; ++i) {
    113         snprintf(buffer, 3, "%02X", digest.at(i));
    114         buffer += 2;
    115     }
    116     return result;
    117 }
    118 
    119 CString SHA1::computeHexDigest()
    120 {
    121     Digest digest;
    122     computeHash(digest);
    123     return hexDigest(digest);
    124 }
    125 
    126127void SHA1::finalize()
    127128{
     
    196197}
    197198
     199#endif
     200
     201CString SHA1::hexDigest(const Digest& digest)
     202{
     203    char* start = 0;
     204    CString result = CString::newUninitialized(40, start);
     205    char* buffer = start;
     206    for (size_t i = 0; i < hashSize; ++i) {
     207        snprintf(buffer, 3, "%02X", digest.at(i));
     208        buffer += 2;
     209    }
     210    return result;
     211}
     212
     213CString SHA1::computeHexDigest()
     214{
     215    Digest digest;
     216    computeHash(digest);
     217    return hexDigest(digest);
     218}
     219
    198220} // namespace WTF
  • trunk/Source/WTF/wtf/SHA1.h

    r160456 r182895  
    3636#include <wtf/text/CString.h>
    3737
     38#if PLATFORM(COCOA)
     39#include <CommonCrypto/CommonDigest.h>
     40#endif
     41
    3842namespace WTF {
    3943
     
    6468    typedef std::array<uint8_t, hashSize> Digest;
    6569
    66     // computeHash has a side effect of resetting the state of the object.
    6770    WTF_EXPORT_PRIVATE void computeHash(Digest&);
    6871   
     
    7477
    7578private:
     79#if PLATFORM(COCOA)
     80    CC_SHA1_CTX m_context;
     81#else
    7682    void finalize();
    7783    void processBlock();
     
    8288    uint64_t m_totalBytes; // Number of bytes added so far.
    8389    uint32_t m_hash[5];
     90#endif
    8491};
    8592
Note: See TracChangeset for help on using the changeset viewer.