//
// Getdown - application installer, patcher and launcher
// Copyright (C) 2004-2018 Getdown authors
// https://github.com/threerings/getdown/blob/master/LICENSE
package com.threerings.getdown.data;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.*;
import com.threerings.getdown.util.Config;
import com.threerings.getdown.util.MessageUtil;
import com.threerings.getdown.util.ProgressObserver;
import com.threerings.getdown.util.StringUtil;
import static com.threerings.getdown.Log.log;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Manages the digest.txt file and the computing and processing of digests for an
* application.
*/
public class Digest
{
/** The current version of the digest protocol. */
public static final int VERSION = 2;
/**
* Returns the name of the digest file for the specified protocol version.
*/
public static String digestFile (int version) {
String infix = version > 1 ? String.valueOf(version) : "";
return FILE_NAME + infix + FILE_SUFFIX;
}
/**
* Returns the crypto algorithm used to sign digest files of the specified version.
*/
public static String sigAlgorithm (int version) {
switch (version) {
case 1: return "SHA1withRSA";
case 2: return "SHA256withRSA";
default: throw new IllegalArgumentException("Invalid digest version " + version);
}
}
/**
* Creates a digest file at the specified location using the supplied list of resources.
* @param version the version of the digest protocol to use.
*/
public static void createDigest (int version, List resources, File output)
throws IOException
{
// first compute the digests for all the resources in parallel
ExecutorService exec = Executors.newFixedThreadPool(SysProps.threadPoolSize());
final Map digests = new ConcurrentHashMap<>();
final BlockingQueue