Bug 1433003 - Optimize sync by using digestBytes instead of digestUTF8. r?markh draft
authorThom Chiovoloni <tchiovoloni@mozilla.com>
Wed, 24 Jan 2018 20:09:16 -0500
changeset 724474 fc0f62f4ede1d7f2dd9804a505a40363a94ad238
parent 724332 e22e2c9eb81686e958a9448ea3d1e8cd766743e2
child 747160 8fe0ea024ac43442c1c53d08bf8947802be46e5c
push id96756
push userbmo:tchiovoloni@mozilla.com
push dateThu, 25 Jan 2018 01:09:44 +0000
reviewersmarkh
bugs1433003
milestone60.0a1
Bug 1433003 - Optimize sync by using digestBytes instead of digestUTF8. r?markh MozReview-Commit-ID: IbyeOUjKdi9
services/crypto/modules/utils.js
services/sync/modules/record.js
--- a/services/crypto/modules/utils.js
+++ b/services/crypto/modules/utils.js
@@ -51,17 +51,20 @@ this.CryptoUtils = {
 
   /**
    * Treat the given message as a bytes string and hash it with the given
    * hasher. Returns a string containing bytes. The hasher is reset if it's
    * an HMAC hasher.
    */
   digestBytes: function digestBytes(message, hasher) {
     // No UTF-8 encoding for you, sunshine.
-    let bytes = Array.prototype.slice.call(message).map(b => b.charCodeAt(0));
+    let bytes = new Uint8Array(message.length);
+    for (let i = 0; i < message.length; ++i) {
+      bytes[i] = message.charCodeAt(i) & 0xff;
+    }
     hasher.update(bytes, bytes.length);
     let result = hasher.finish(false);
     if (hasher instanceof Ci.nsICryptoHMAC) {
       hasher.reset();
     }
     return result;
   },
 
--- a/services/sync/modules/record.js
+++ b/services/sync/modules/record.js
@@ -119,17 +119,17 @@ CryptoWrapper.prototype = {
   _logName: "Sync.Record.CryptoWrapper",
 
   ciphertextHMAC: function ciphertextHMAC(keyBundle) {
     let hasher = keyBundle.sha256HMACHasher;
     if (!hasher) {
       throw new Error("Cannot compute HMAC without an HMAC key.");
     }
 
-    return CommonUtils.bytesAsHex(Utils.digestUTF8(this.ciphertext, hasher));
+    return CommonUtils.bytesAsHex(Utils.digestBytes(this.ciphertext, hasher));
   },
 
   /*
    * Don't directly use the sync key. Instead, grab a key for this
    * collection, which is decrypted with the sync key.
    *
    * Cache those keys; invalidate the cache if the time on the keys collection
    * changes, or other auth events occur.