Skip to content

Instantly share code, notes, and snippets.

@botic
Created April 7, 2017 08:54
Show Gist options
  • Select an option

  • Save botic/13e319451e638a6cea24a001dc0a011f to your computer and use it in GitHub Desktop.

Select an option

Save botic/13e319451e638a6cea24a001dc0a011f to your computer and use it in GitHub Desktop.
Create a checksum for assets
exports.createChecksum = function(asset, algorithm) {
const md = MessageDigest.getInstance(algorithm);
let digest;
if (asset instanceof ByteArray) {
digest = md.digest(asset);
} else if (typeof asset === "string") {
let fis = new java.io.FileInputStream(asset);
let dis = new java.security.DigestInputStream(fis, md);
let buffer = new ByteArray(8192);
try {
while (dis.read(buffer) != -1);
} catch (e) {
throw new Error("Could not create checksum! " + e);
} finally {
buffer = null;
fis.close();
dis.close();
}
digest = md.digest();
} else {
throw new Error("Invalid type! " + typeof asset);
}
return algorithm.toLowerCase() + "-" + b16encode(ByteString.wrap(digest));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment