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

Unified Diff: third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/common/ObjectIdDigestUtils.java

Issue 1162033004: Pull cacheinvalidations code directory into chromium repo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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: third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/common/ObjectIdDigestUtils.java
diff --git a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/common/ObjectIdDigestUtils.java b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/common/ObjectIdDigestUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..248803ed564ee924e285f987e988866fd0bd1a61
--- /dev/null
+++ b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/common/ObjectIdDigestUtils.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.ipc.invalidation.common;
+
+import com.google.ipc.invalidation.util.Bytes;
+import com.google.ipc.invalidation.util.Preconditions;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * Digest-related utilities for object ids.
+ *
+ */
+public class ObjectIdDigestUtils {
+ /**
+ * Implementation of {@link DigestFunction} using SHA-1.
+ */
+ public static class Sha1DigestFunction
+ implements DigestFunction {
+ /** Digest implementation. */
+ private MessageDigest sha1;
+
+ /**
+ * Whether the {@link #reset()} method needs to be called. This is set to true
+ * when {@link #getDigest()} is called and aims to prevent subtle bugs caused by
+ * failing to reset the object before computing a new digest.
+ */
+ private boolean resetNeeded = false;
+
+ public Sha1DigestFunction() {
+ try {
+ this.sha1 = MessageDigest.getInstance("SHA-1");
+ } catch (NoSuchAlgorithmException exception) {
+ throw new RuntimeException(exception);
+ }
+ }
+
+ @Override
+ public void reset() {
+ resetNeeded = false;
+ sha1.reset();
+ }
+
+ @Override
+ public void update(byte[] data) {
+ Preconditions.checkState(!resetNeeded);
+ sha1.update(data);
+ }
+
+ @Override
+ public byte[] getDigest() {
+ Preconditions.checkState(!resetNeeded);
+ resetNeeded = true;
+ return sha1.digest();
+ }
+ }
+
+ /**
+ * Returns the digest of {@code objectIdDigests} using {@code digestFn}.
+ * <p>
+ * REQUIRES: {@code objectIdDigests} iterate in sorted order.
+ */
+ public static Bytes getDigest(Iterable<Bytes> objectIdDigests, DigestFunction digestFn) {
+ digestFn.reset();
+ for (Bytes objectIdDigest : objectIdDigests) {
+ digestFn.update(objectIdDigest.getByteArray());
+ }
+ return new Bytes(digestFn.getDigest());
+ }
+
+ /**
+ * Returns the digest for the object id with source {@code objectSource} and name
+ * {@code objectName} using {@code digestFn}.
+ */
+ public static Bytes getDigest(int objectSource, byte[] objectName, DigestFunction digestFn) {
+ digestFn.reset();
+ ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / 8).order(ByteOrder.LITTLE_ENDIAN);
+ buffer.putInt(objectSource);
+
+ // Little endian number for type followed by bytes.
+ digestFn.update(buffer.array());
+ digestFn.update(objectName);
+ return new Bytes(digestFn.getDigest());
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698