Class SHASum
- java.lang.Object
-
- com.jogamp.common.util.SHASum
-
- Direct Known Subclasses:
SHASum.TempJarSHASum
public class SHASum extends Object
Utility class to produce secure hash (SHA) sums over diverse input sources.See
updateDigest(MessageDigest, List)
This implementation is being utilized at JogAmp build time to produce various SHA sums over sources, class files and native libraries to ensure their identity. See
JogampVersion.getImplementationSHASources()
,JogampVersion.getImplementationSHAClasses()
andJogampVersion.getImplementationSHANatives()
.JogampVersion.getImplementationSHASources()
for module gluegen is produced via:java -cp build/gluegen-rt.jar com.jogamp.common.util.SHASum --algorithm 256 --exclude ".*\\.log" --exclude "make/lib/toolchain" src jcpp/src make
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SHASum.TempJarSHASum
SHASum
specialization utilizingTempJarCache
to access jar file content for SHA computation
-
Constructor Summary
Constructors Constructor Description SHASum(MessageDigest digest, List<String> origins, List<Pattern> excludes, List<Pattern> includes)
Instance to ensure propercompute(boolean)
of identical SHA sums over same contents within given paths across machines.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
compute(boolean verbose)
Implementation gathers all files traversing through given paths viaIOUtil.filesOf(List, List, List)
, sorts the resulting file list viasort(ArrayList)
and finally calculates the SHA sum over its byte content viaupdateDigest(MessageDigest, List)
.List<Pattern>
getExcludes()
List<Pattern>
getIncludes()
List<String>
getOrigins()
static void
main(String[] args)
Main entry point taking var-arg path or gnu-arguments with a leading '--'.static List<String>
sort(ArrayList<String> source)
Returns the sorted list of given strings usingString.compareTo(String)
's lexicographically comparison.static StringBuilder
toHexString(byte[] shasum, StringBuilder sb)
Simple helper to print the given byte-array into a string, here appended to StringBuilderstatic long
updateDigest(MessageDigest digest, List<String> filenames)
Updates
the givendigest
with the bytes contained by the files denoted by the givenfilenames
in the given order.
-
-
-
Constructor Detail
-
SHASum
public SHASum(MessageDigest digest, List<String> origins, List<Pattern> excludes, List<Pattern> includes)
Instance to ensure propercompute(boolean)
of identical SHA sums over same contents within given paths across machines.Instantiation of this class is lightweight,
compute(boolean)
performs all operations.- Parameters:
digest
- the SHA algorithmorigins
- the mandatory path origins to be used forIOUtil.filesOf(List, List, List)
excludes
- the optional exclude patterns to be used forIOUtil.filesOf(List, List, List)
includes
- the optional include patterns to be used forIOUtil.filesOf(List, List, List)
- Throws:
IllegalArgumentException
IOException
URISyntaxException
-
-
Method Detail
-
updateDigest
public static long updateDigest(MessageDigest digest, List<String> filenames) throws IOException
Updates
the givendigest
with the bytes contained by the files denoted by the givenfilenames
in the given order.To retrieve the list of all files traversing through directories, one may use
IOUtil.filesOf(List, List, List)
.The SHA implementation is sensitive to the order of input bytes and hence the given filename order.
It is advised to pass given list of filenames in lexicographically sorted order to ensure reproducible outcome across all platforms, one may use
sort(ArrayList)
.As an example, one could write
final MessageDigest digest = ...; final long totalBytes = updateDigest(digest, sort(IOUtil.filesOf(Arrays.asList("sources"), null, null)));
- Parameters:
digest
- to be updated digestfilenames
- list of filenames denoting files, which bytes will be used to update the digest- Returns:
- total number of bytes read.
- Throws:
FileNotFoundException
- seeFileInputStream(String)
IOException
- seeInputStream.read(byte[])
-
toHexString
public static StringBuilder toHexString(byte[] shasum, StringBuilder sb)
Simple helper to print the given byte-array into a string, here appended to StringBuilder- Parameters:
shasum
- the given byte-arraysb
- optional pre-existing StringBuilder, may be null- Returns:
- return given or new StringBuilder with appended hex-string
-
sort
public static List<String> sort(ArrayList<String> source)
Returns the sorted list of given strings usingString.compareTo(String)
's lexicographically comparison.- Parameters:
source
- given input strings- Returns:
- sorted list of given strings
-
compute
public final byte[] compute(boolean verbose) throws IOException
Implementation gathers all files traversing through given paths viaIOUtil.filesOf(List, List, List)
, sorts the resulting file list viasort(ArrayList)
and finally calculates the SHA sum over its byte content viaupdateDigest(MessageDigest, List)
.This ensures identical SHA sums over same contents within given paths across machines.
This method is heavyweight and performs all operations.
- Parameters:
verbose
- if true, all used files will be dumped as well as the digest result- Returns:
- the resulting SHA value
- Throws:
IOException
-
main
public static void main(String[] args) throws IOException
Main entry point taking var-arg path or gnu-arguments with a leading '--'.Implementation gathers all files traversing through given paths via
IOUtil.filesOf(List, List, List)
, sorts the resulting file list viasort(ArrayList)
and finally calculates the SHA sum over its byte content viaupdateDigest(MessageDigest, List)
. This ensures identical SHA sums over same contents within given paths.Example to calculate the SHA-256 over our source files as performed for
JogampVersion.getImplementationSHASources()
java -cp build/gluegen-rt.jar com.jogamp.common.util.SHASum --algorithm 256 --exclude ".*\\.log" --exclude "make/lib/toolchain" src jcpp/src make
To validate the implementation, one can gather the sorted list of files (to ensure same order)
java -cp build/gluegen-rt.jar com.jogamp.common.util.SHASum --listfilesonly --exclude ".*\\.log" --exclude "make/lib/toolchain" src jcpp/src make >& java.sorted.txt
and then calculate the shasum independentlyfind `cat java.sorted.txt` -exec cat {} + | shasum -a 256 -b - | awk '{print $1}'
- Parameters:
args
-- Throws:
IOException
URISyntaxException
IllegalArgumentException
-
-