@@ -146,134 +146,6 @@ public static byte[] digest(final MessageDigest messageDigest, final RandomAcces
146146 return updateDigest (messageDigest , data ).digest ();
147147 }
148148
149- /**
150- * Reads through a byte array and return a generalized Git blob identifier
151- *
152- * <p>The identifier is computed in the way described by the
153- * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#52-contents">SWHID contents identifier</a>, but it can use any hash
154- * algorithm.</p>
155- *
156- * <p>When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.</p>
157- *
158- * @param messageDigest The MessageDigest to use (for example SHA-1).
159- * @param data Data to digest.
160- * @return A generalized Git blob identifier.
161- * @since 1.22.0
162- */
163- public static byte [] gitBlob (final MessageDigest messageDigest , final byte [] data ) {
164- messageDigest .reset ();
165- updateDigest (messageDigest , gitBlobPrefix (data .length ));
166- return digest (messageDigest , data );
167- }
168-
169- /**
170- * Reads through a byte array and return a generalized Git blob identifier
171- *
172- * <p>The identifier is computed in the way described by the
173- * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#52-contents">SWHID contents identifier</a>, but it can use any hash
174- * algorithm.</p>
175- *
176- * <p>When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.</p>
177- *
178- * @param messageDigest The MessageDigest to use (for example SHA-1).
179- * @param data Data to digest.
180- * @param options Options how to open the file
181- * @return A generalized Git blob identifier.
182- * @throws IOException On error accessing the file
183- * @since 1.22.0
184- */
185- public static byte [] gitBlob (final MessageDigest messageDigest , final Path data , final OpenOption ... options ) throws IOException {
186- messageDigest .reset ();
187- updateDigest (messageDigest , gitBlobPrefix (Files .size (data )));
188- return updateDigest (messageDigest , data , options ).digest ();
189- }
190-
191- private static byte [] gitBlobPrefix (final long dataSize ) {
192- return ("blob " + dataSize + "\0 " ).getBytes (StandardCharsets .UTF_8 );
193- }
194-
195- /**
196- * Returns a generalized Git tree identifier
197- *
198- * <p>The identifier is computed in the way described by the
199- * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#53-directories">SWHID directory identifier</a>, but it can use any hash
200- * algorithm.</p>
201- *
202- * <p>When the hash algorithm is SHA-1, the identifier is identical to Git tree identifier and SWHID directory identifier.</p>
203- *
204- * @param messageDigest The MessageDigest to use (for example SHA-1)
205- * @param entries The directory entries
206- * @return A generalized Git tree identifier.
207- */
208- static byte [] gitTree (final MessageDigest messageDigest , final Collection <GitDirectoryEntry > entries ) {
209- final TreeSet <GitDirectoryEntry > treeSet = new TreeSet <>(entries );
210- final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
211- for (final GitDirectoryEntry entry : treeSet ) {
212- final byte [] treeEntryBytes = entry .toTreeEntryBytes ();
213- baos .write (treeEntryBytes , 0 , treeEntryBytes .length );
214- }
215- messageDigest .reset ();
216- updateDigest (messageDigest , gitTreePrefix (baos .size ()));
217- return updateDigest (messageDigest , baos .toByteArray ()).digest ();
218- }
219-
220- /**
221- * Reads through a byte array and return a generalized Git tree identifier
222- *
223- * <p>The identifier is computed in the way described by the
224- * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#53-directories">SWHID directory identifier</a>, but it can use any hash
225- * algorithm.</p>
226- *
227- * <p>When the hash algorithm is SHA-1, the identifier is identical to Git tree identifier and SWHID directory identifier.</p>
228- *
229- * @param messageDigest The MessageDigest to use (for example SHA-1).
230- * @param data Data to digest.
231- * @param options Options how to open the file
232- * @return A generalized Git tree identifier.
233- * @throws IOException On error accessing the file
234- * @since 1.22.0
235- */
236- public static byte [] gitTree (final MessageDigest messageDigest , final Path data , final OpenOption ... options ) throws IOException {
237- final List <GitDirectoryEntry > entries = new ArrayList <>();
238- try (DirectoryStream <Path > files = Files .newDirectoryStream (data )) {
239- for (final Path path : files ) {
240- final GitDirectoryEntry .Type type = getGitDirectoryEntryType (path );
241- final byte [] rawObjectId ;
242- if (type == GitDirectoryEntry .Type .DIRECTORY ) {
243- rawObjectId = gitTree (messageDigest , path , options );
244- } else {
245- rawObjectId = gitBlob (messageDigest , path , options );
246- }
247- entries .add (new GitDirectoryEntry (path , type , rawObjectId ));
248- }
249- }
250- return gitTree (messageDigest , entries );
251- }
252-
253- /**
254- * Returns the {@link GitDirectoryEntry.Type} of a file.
255- *
256- * @param path The file to check.
257- * @return A {@link GitDirectoryEntry.Type}
258- */
259- private static GitDirectoryEntry .Type getGitDirectoryEntryType (final Path path ) {
260- // Symbolic links first
261- if (Files .isSymbolicLink (path )) {
262- return GitDirectoryEntry .Type .SYMBOLIC_LINK ;
263- }
264- if (Files .isDirectory (path )) {
265- return GitDirectoryEntry .Type .DIRECTORY ;
266- }
267- if (Files .isExecutable (path )) {
268- return GitDirectoryEntry .Type .EXECUTABLE ;
269- }
270- return GitDirectoryEntry .Type .REGULAR ;
271- }
272-
273- private static byte [] gitTreePrefix (final long dataSize ) {
274- return ("tree " + dataSize + "\0 " ).getBytes (StandardCharsets .UTF_8 );
275- }
276-
277149 /**
278150 * Gets a {@code MessageDigest} for the given {@code algorithm}.
279151 *
@@ -312,6 +184,26 @@ public static MessageDigest getDigest(final String algorithm, final MessageDiges
312184 }
313185 }
314186
187+ /**
188+ * Returns the {@link GitDirectoryEntry.Type} of a file.
189+ *
190+ * @param path The file to check.
191+ * @return A {@link GitDirectoryEntry.Type}
192+ */
193+ private static GitDirectoryEntry .Type getGitDirectoryEntryType (final Path path ) {
194+ // Symbolic links first
195+ if (Files .isSymbolicLink (path )) {
196+ return GitDirectoryEntry .Type .SYMBOLIC_LINK ;
197+ }
198+ if (Files .isDirectory (path )) {
199+ return GitDirectoryEntry .Type .DIRECTORY ;
200+ }
201+ if (Files .isExecutable (path )) {
202+ return GitDirectoryEntry .Type .EXECUTABLE ;
203+ }
204+ return GitDirectoryEntry .Type .REGULAR ;
205+ }
206+
315207 /**
316208 * Gets an MD2 MessageDigest.
317209 *
@@ -508,6 +400,114 @@ public static MessageDigest getShake256_512Digest() {
508400 return getDigest (MessageDigestAlgorithms .SHAKE256_512 );
509401 }
510402
403+ /**
404+ * Reads through a byte array and return a generalized Git blob identifier
405+ *
406+ * <p>The identifier is computed in the way described by the
407+ * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#52-contents">SWHID contents identifier</a>, but it can use any hash
408+ * algorithm.</p>
409+ *
410+ * <p>When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.</p>
411+ *
412+ * @param messageDigest The MessageDigest to use (for example SHA-1).
413+ * @param data Data to digest.
414+ * @return A generalized Git blob identifier.
415+ * @since 1.22.0
416+ */
417+ public static byte [] gitBlob (final MessageDigest messageDigest , final byte [] data ) {
418+ messageDigest .reset ();
419+ updateDigest (messageDigest , gitBlobPrefix (data .length ));
420+ return digest (messageDigest , data );
421+ }
422+
423+ /**
424+ * Reads through a byte array and return a generalized Git blob identifier
425+ *
426+ * <p>The identifier is computed in the way described by the
427+ * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#52-contents">SWHID contents identifier</a>, but it can use any hash
428+ * algorithm.</p>
429+ *
430+ * <p>When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.</p>
431+ *
432+ * @param messageDigest The MessageDigest to use (for example SHA-1).
433+ * @param data Data to digest.
434+ * @param options Options how to open the file
435+ * @return A generalized Git blob identifier.
436+ * @throws IOException On error accessing the file
437+ * @since 1.22.0
438+ */
439+ public static byte [] gitBlob (final MessageDigest messageDigest , final Path data , final OpenOption ... options ) throws IOException {
440+ messageDigest .reset ();
441+ updateDigest (messageDigest , gitBlobPrefix (Files .size (data )));
442+ return updateDigest (messageDigest , data , options ).digest ();
443+ }
444+
445+ private static byte [] gitBlobPrefix (final long dataSize ) {
446+ return ("blob " + dataSize + "\0 " ).getBytes (StandardCharsets .UTF_8 );
447+ }
448+
449+ /**
450+ * Returns a generalized Git tree identifier
451+ *
452+ * <p>The identifier is computed in the way described by the
453+ * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#53-directories">SWHID directory identifier</a>, but it can use any hash
454+ * algorithm.</p>
455+ *
456+ * <p>When the hash algorithm is SHA-1, the identifier is identical to Git tree identifier and SWHID directory identifier.</p>
457+ *
458+ * @param messageDigest The MessageDigest to use (for example SHA-1)
459+ * @param entries The directory entries
460+ * @return A generalized Git tree identifier.
461+ */
462+ static byte [] gitTree (final MessageDigest messageDigest , final Collection <GitDirectoryEntry > entries ) {
463+ final TreeSet <GitDirectoryEntry > treeSet = new TreeSet <>(entries );
464+ final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
465+ for (final GitDirectoryEntry entry : treeSet ) {
466+ final byte [] treeEntryBytes = entry .toTreeEntryBytes ();
467+ baos .write (treeEntryBytes , 0 , treeEntryBytes .length );
468+ }
469+ messageDigest .reset ();
470+ updateDigest (messageDigest , gitTreePrefix (baos .size ()));
471+ return updateDigest (messageDigest , baos .toByteArray ()).digest ();
472+ }
473+
474+ /**
475+ * Reads through a byte array and return a generalized Git tree identifier
476+ *
477+ * <p>The identifier is computed in the way described by the
478+ * <a href="https://www.swhid.org/swhid-specification/v1.2/5.Core_identifiers/#53-directories">SWHID directory identifier</a>, but it can use any hash
479+ * algorithm.</p>
480+ *
481+ * <p>When the hash algorithm is SHA-1, the identifier is identical to Git tree identifier and SWHID directory identifier.</p>
482+ *
483+ * @param messageDigest The MessageDigest to use (for example SHA-1).
484+ * @param data Data to digest.
485+ * @param options Options how to open the file
486+ * @return A generalized Git tree identifier.
487+ * @throws IOException On error accessing the file
488+ * @since 1.22.0
489+ */
490+ public static byte [] gitTree (final MessageDigest messageDigest , final Path data , final OpenOption ... options ) throws IOException {
491+ final List <GitDirectoryEntry > entries = new ArrayList <>();
492+ try (DirectoryStream <Path > files = Files .newDirectoryStream (data )) {
493+ for (final Path path : files ) {
494+ final GitDirectoryEntry .Type type = getGitDirectoryEntryType (path );
495+ final byte [] rawObjectId ;
496+ if (type == GitDirectoryEntry .Type .DIRECTORY ) {
497+ rawObjectId = gitTree (messageDigest , path , options );
498+ } else {
499+ rawObjectId = gitBlob (messageDigest , path , options );
500+ }
501+ entries .add (new GitDirectoryEntry (path , type , rawObjectId ));
502+ }
503+ }
504+ return gitTree (messageDigest , entries );
505+ }
506+
507+ private static byte [] gitTreePrefix (final long dataSize ) {
508+ return ("tree " + dataSize + "\0 " ).getBytes (StandardCharsets .UTF_8 );
509+ }
510+
511511 /**
512512 * Test whether the algorithm is supported.
513513 *
0 commit comments