Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Unified Diff: Source/platform/Crypto.cpp

Issue 189373010: Get rid of WTF::SHA1. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Use new digest-by-chunk API and many other fixes Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/platform/Crypto.cpp
diff --git a/Source/platform/Crypto.cpp b/Source/platform/Crypto.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..8a446dfb6834705531d0b224506536d378dc760a
--- /dev/null
+++ b/Source/platform/Crypto.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2014 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Google, Inc. ("Google") nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
abarth-chromium 2014/04/01 23:46:48 Please use new-style copyright blocks: http://dev
jww 2014/04/02 01:11:50 Done.
+
+#include "config.h"
+#include "platform/Crypto.h"
+
+#include "public/platform/Platform.h"
+#include "public/platform/WebArrayBuffer.h"
+#include "public/platform/WebCrypto.h"
+#include "public/platform/WebCryptoAlgorithm.h"
+#include "wtf/Vector.h"
+#include "wtf/text/CString.h"
+
+namespace WebCore {
+
+static blink::WebCryptoAlgorithmId convertHashAlgorithm(HashAlgorithm algorithm)
abarth-chromium 2014/04/01 23:46:48 convertHashAlgorithm -> toWebCryptoAlgorithmId
jww 2014/04/02 01:11:50 Done.
+{
+ switch (algorithm) {
+ case HashAlgorithmSha1:
+ return blink::WebCryptoAlgorithmIdSha1;
+ case HashAlgorithmSha256:
+ return blink::WebCryptoAlgorithmIdSha256;
+ case HashAlgorithmSha384:
+ return blink::WebCryptoAlgorithmIdSha384;
+ case HashAlgorithmSha512:
+ return blink::WebCryptoAlgorithmIdSha512;
+ };
+
+ ASSERT_NOT_REACHED();
+}
+
+void computeDigest(HashAlgorithm algorithm, const char* digestable, size_t length, DigestValue& digestResult)
+{
+ blink::WebCryptoAlgorithmId algorithmId = convertHashAlgorithm(algorithm);
+ blink::WebCrypto* crypto = blink::Platform::current()->crypto();
+ blink::WebArrayBuffer result;
+
+ ASSERT(crypto);
+
+ crypto->digestSynchronous(algorithmId, reinterpret_cast<const unsigned char*>(digestable), length, result);
+
+ ASSERT(!result.isNull());
+
+ digestResult.append(static_cast<uint8_t*>(result.data()), result.byteLength());
+}
+
+blink::WebCryptoDigestor* createDigestor(HashAlgorithm algorithm)
+{
+ blink::WebCrypto* crypto = blink::Platform::current()->crypto();
+
+ ASSERT(crypto);
+
+ return crypto->createDigestor(convertHashAlgorithm(algorithm));
abarth-chromium 2014/04/01 23:46:48 I'd just make this function all be one line. This
jww 2014/04/02 01:11:50 Done.
+}
+
+void finishDigestor(blink::WebCryptoDigestor* digestor, DigestValue& digestResult)
+{
+ unsigned char* result;
+ unsigned resultSize;
abarth-chromium 2014/04/01 23:46:48 Please initialize scalars.
jww 2014/04/02 01:11:50 Done.
+
+ if (!digestor->finish(result, resultSize))
+ return;
+
+ ASSERT(result);
+
+ digestResult.append(static_cast<uint8_t*>(result), resultSize);
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698