aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/actions/cache/Md5Digest.java
blob: 75441b52e78193d4b8235be26d894f265b4906d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.lib.actions.cache;

import com.google.common.hash.HashCode;
import com.google.devtools.build.lib.util.Preconditions;
import java.util.Arrays;

/** A value class for capturing and comparing MD5-based digests. */
public class Md5Digest {

  static final int MD5_SIZE = 16;
  private final byte[] digest;

  /**
   * Construct the digest from the given bytes.
   *
   * @param digest an MD5 digest. Must be sized properly.
   */
  public Md5Digest(byte[] digest) {
    Preconditions.checkState(digest.length == MD5_SIZE);
    this.digest = digest;
  }

  @Override
  public int hashCode() {
    // We are already dealing with the digest so we can just use portion of it as a hash code.
    return digest[0] + (digest[1] << 8) + (digest[2] << 16) + (digest[3] << 24);
  }

  @Override
  public boolean equals(Object obj) {
    return (obj instanceof Md5Digest) && Arrays.equals(digest, ((Md5Digest) obj).digest);
  }

  @Override
  public String toString() {
    return HashCode.fromBytes(digest).toString();
  }

  /**
   * Warning: This is a mutable wrapper and does not necessarily own the underlying byte[]. Don't
   * modify it unless you are sure about it.
   */
  public byte[] getDigestBytesUnsafe() {
    return digest;
  }
}