1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.lfs.test;
11
12 import static java.nio.charset.StandardCharsets.UTF_8;
13
14 import java.io.BufferedInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.security.MessageDigest;
21
22 import org.eclipse.jgit.lfs.lib.Constants;
23 import org.eclipse.jgit.lfs.lib.LongObjectId;
24
25 public class LongObjectIdTestUtils {
26
27
28
29
30
31
32
33
34 public static LongObjectId hash(String s) {
35 MessageDigest md = Constants.newMessageDigest();
36 md.update(s.getBytes(UTF_8));
37 return LongObjectId.fromRaw(md.digest());
38 }
39
40
41
42
43
44
45
46
47
48
49
50 public static LongObjectId hash(Path file)
51 throws FileNotFoundException, IOException {
52 MessageDigest md = Constants.newMessageDigest();
53 try (InputStream is = new BufferedInputStream(
54 Files.newInputStream(file))) {
55 final byte[] buffer = new byte[4096];
56 for (int read = 0; (read = is.read(buffer)) != -1;) {
57 md.update(buffer, 0, read);
58 }
59 }
60 return LongObjectId.fromRaw(md.digest());
61 }
62 }