예제 #1
0
 /** Get a Md5 string which is similar to OS Md5sum */
 public static String md5(File file) {
   try {
     HashCode hc = Files.hash(file, Hashing.md5());
     return toHex(hc.asBytes());
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
예제 #2
0
 @Override
 public boolean equals(@Nullable Object object) {
   if (object instanceof HashCode) {
     HashCode that = (HashCode) object;
     // Undocumented: this is a non-short-circuiting equals(), in case this is a cryptographic
     // hash code, in which case we don't want to leak timing information
     return MessageDigest.isEqual(this.asBytes(), that.asBytes());
   }
   return false;
 }
예제 #3
0
파일: RuleKey.java 프로젝트: Nouhi/buck
 /**
  * Order non-idempotent RuleKeys as less than all idempotent RuleKeys.
  *
  * <p>non-idempotent < idempotent
  */
 @Override
 public int compareTo(RuleKey other) {
   if (!isIdempotent()) {
     if (!other.isIdempotent()) {
       return 0;
     }
     return -1;
   } else if (!other.isIdempotent()) {
     return 1;
   }
   return ByteBuffer.wrap(hashCode.asBytes()).compareTo(ByteBuffer.wrap(other.hashCode.asBytes()));
 }
예제 #4
0
 public static HashCode combineOrdered(Iterable<HashCode> hashCodes) {
   Iterator<HashCode> iterator = hashCodes.iterator();
   Preconditions.checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
   int bits = ((HashCode) iterator.next()).bits();
   byte[] resultBytes = new byte[bits / 8];
   for (HashCode hashCode : hashCodes) {
     byte[] nextBytes = hashCode.asBytes();
     Preconditions.checkArgument(
         nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
     for (int i = 0; i < nextBytes.length; i++) {
       resultBytes[i] = ((byte) (resultBytes[i] * 37 ^ nextBytes[i]));
     }
   }
   return HashCode.fromBytesNoCopy(resultBytes);
 }
예제 #5
0
 public static HashCode combineUnordered(Iterable<HashCode> hashCodes) {
   Iterator<HashCode> iterator = hashCodes.iterator();
   Preconditions.checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
   byte[] resultBytes = new byte[((HashCode) iterator.next()).bits() / 8];
   for (HashCode hashCode : hashCodes) {
     byte[] nextBytes = hashCode.asBytes();
     Preconditions.checkArgument(
         nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
     for (int i = 0; i < nextBytes.length; tmp102_100++) {
       int tmp102_100 = i;
       byte[] tmp102_99 = resultBytes;
       tmp102_99[tmp102_100] = ((byte) (tmp102_99[tmp102_100] + nextBytes[tmp102_100]));
     }
   }
   return HashCode.fromBytesNoCopy(resultBytes);
 }
예제 #6
0
파일: RuleKey.java 프로젝트: Nouhi/buck
 private Builder setVal(@Nullable File file) {
   if (file != null) {
     // Compute a separate SHA-1 for the file contents and feed that into messageDigest rather
     // than the file contents, in order to avoid the overhead of escaping SEPARATOR in the file
     // content.
     try {
       byte[] fileBytes = Files.toByteArray(file);
       HashCode fileSha1 = Hashing.sha1().hashBytes(fileBytes);
       if (logElms != null) {
         logElms.add(
             String.format("file(path=\"%s\", sha1=%s):", file.getPath(), fileSha1.toString()));
       }
       feed(fileSha1.asBytes());
     } catch (IOException e) {
       // The file is nonexistent/unreadable; generate a RuleKey that prevents accidental
       // caching.
       if (logElms != null) {
         logElms.add(String.format("file(path=\"%s\", sha1=random):", file.getPath()));
       }
       nonIdempotent();
     }
   }
   return separate();
 }
예제 #7
0
 private byte[] hashExtId(EventExtId extId) {
   HashCode hashCode = Hashing.sha1().hashString(extId.getExtId(), Charsets.US_ASCII);
   return hashCode.asBytes();
 }
  @Override
  public String putBlob(final String containerName, final Blob blob) throws IOException {
    String blobKey = blob.getMetadata().getName();
    Payload payload = blob.getPayload();
    filesystemContainerNameValidator.validate(containerName);
    filesystemBlobKeyValidator.validate(blobKey);
    if (getDirectoryBlobSuffix(blobKey) != null) {
      return putDirectoryBlob(containerName, blob);
    }
    File outputFile = getFileForBlobKey(containerName, blobKey);
    // TODO: should we use a known suffix to filter these out during list?
    String tmpBlobName = blobKey + "-" + UUID.randomUUID();
    File tmpFile = getFileForBlobKey(containerName, tmpBlobName);
    Path tmpPath = tmpFile.toPath();
    HashingInputStream his = null;
    try {
      Files.createParentDirs(tmpFile);
      his = new HashingInputStream(Hashing.md5(), payload.openStream());
      long actualSize = Files.asByteSink(tmpFile).writeFrom(his);
      Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength();
      if (expectedSize != null && actualSize != expectedSize) {
        throw new IOException(
            "Content-Length mismatch, actual: " + actualSize + " expected: " + expectedSize);
      }
      HashCode actualHashCode = his.hash();
      HashCode expectedHashCode = payload.getContentMetadata().getContentMD5AsHashCode();
      if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) {
        throw new IOException(
            "MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode);
      }
      payload.getContentMetadata().setContentMD5(actualHashCode);

      if (outputFile.exists()) {
        delete(outputFile);
      }

      UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(tmpPath);
      if (view != null) {
        try {
          view.write(XATTR_CONTENT_MD5, ByteBuffer.wrap(actualHashCode.asBytes()));
          writeCommonMetadataAttr(view, blob);
        } catch (IOException e) {
          logger.debug("xattrs not supported on %s", tmpPath);
        }
      }

      setBlobAccess(containerName, tmpBlobName, BlobAccess.PRIVATE);

      if (!tmpFile.renameTo(outputFile)) {
        throw new RuntimeException("Could not rename file " + tmpFile + " to " + outputFile);
      }

      return base16().lowerCase().encode(actualHashCode.asBytes());
    } catch (IOException ex) {
      if (tmpFile != null) {
        try {
          delete(tmpFile);
        } catch (IOException e) {
          logger.debug("Could not delete %s: %s", tmpFile, e);
        }
      }
      throw ex;
    } finally {
      closeQuietly(his);
      if (payload != null) {
        payload.release();
      }
    }
  }
예제 #9
0
 private <T extends Value> HashCode taskHash(Task task) {
   Hasher hasher = Hash.newHasher();
   hasher.putBytes(smoothJarHash.asBytes());
   hasher.putBytes(task.hash().asBytes());
   return hasher.hash();
 }