2 * Copyright 2011 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.ipc.invalidation.common;
19 import com.google.ipc.invalidation.util.Bytes;
20 import com.google.ipc.invalidation.util.Preconditions;
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
28 * Digest-related utilities for object ids.
31 public class ObjectIdDigestUtils {
33 * Implementation of {@link DigestFunction} using SHA-1.
35 public static class Sha1DigestFunction
36 implements DigestFunction {
37 /** Digest implementation. */
38 private MessageDigest sha1;
41 * Whether the {@link #reset()} method needs to be called. This is set to true
42 * when {@link #getDigest()} is called and aims to prevent subtle bugs caused by
43 * failing to reset the object before computing a new digest.
45 private boolean resetNeeded = false;
47 public Sha1DigestFunction() {
49 this.sha1 = MessageDigest.getInstance("SHA-1");
50 } catch (NoSuchAlgorithmException exception) {
51 throw new RuntimeException(exception);
62 public void update(byte[] data) {
63 Preconditions.checkState(!resetNeeded);
68 public byte[] getDigest() {
69 Preconditions.checkState(!resetNeeded);
76 * Returns the digest of {@code objectIdDigests} using {@code digestFn}.
78 * REQUIRES: {@code objectIdDigests} iterate in sorted order.
80 public static Bytes getDigest(Iterable<Bytes> objectIdDigests, DigestFunction digestFn) {
82 for (Bytes objectIdDigest : objectIdDigests) {
83 digestFn.update(objectIdDigest.getByteArray());
85 return new Bytes(digestFn.getDigest());
89 * Returns the digest for the object id with source {@code objectSource} and name
90 * {@code objectName} using {@code digestFn}.
92 public static Bytes getDigest(int objectSource, byte[] objectName, DigestFunction digestFn) {
94 ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / 8).order(ByteOrder.LITTLE_ENDIAN);
95 buffer.putInt(objectSource);
97 // Little endian number for type followed by bytes.
98 digestFn.update(buffer.array());
99 digestFn.update(objectName);
100 return new Bytes(digestFn.getDigest());