forked from jenkinsci/ansicolor-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jenkinsci#185 Hash line content with its ordinal number to uniquely i…
…dentify it
- Loading branch information
Showing
2 changed files
with
33 additions
and
50 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/hudson/plugins/ansicolor/action/LineIdentifier.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hudson.plugins.ansicolor.action; | ||
|
||
import java.io.Serializable; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Base64; | ||
|
||
public class LineIdentifier implements Serializable { | ||
private static final String ALGORITHM = "SHA-256"; | ||
private MessageDigest messageDigest; | ||
|
||
private MessageDigest getMessageDigest() { | ||
if (messageDigest == null) { | ||
try { | ||
messageDigest = MessageDigest.getInstance(ALGORITHM); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException("Cannot get message digest", e); | ||
} | ||
} | ||
return messageDigest; | ||
} | ||
|
||
public String hash(String lineContent, long lineNo) { | ||
final String key = String.join("|", lineContent, String.valueOf(lineNo)); | ||
return Base64.getEncoder().encodeToString(getMessageDigest().digest(key.getBytes())); | ||
} | ||
|
||
public boolean isEqual(String lineContent, long lineNo, String other) { | ||
return hash(lineContent, lineNo).equals(other); | ||
} | ||
} |
This file contains 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