Created
April 7, 2017 08:54
-
-
Save botic/13e319451e638a6cea24a001dc0a011f to your computer and use it in GitHub Desktop.
Create a checksum for assets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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