-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
126 additions
and
27 deletions.
There are no files selected for viewing
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
73 changes: 73 additions & 0 deletions
73
java-service-examples/src/main/java/nbbrd/service/examples/HashAlgorithm.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,73 @@ | ||
package nbbrd.service.examples; | ||
|
||
import nbbrd.service.Quantifier; | ||
import nbbrd.service.ServiceDefinition; | ||
import nbbrd.service.ServiceId; | ||
import nbbrd.service.ServiceProvider; | ||
|
||
import java.math.BigInteger; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Security; | ||
import java.util.Locale; | ||
import java.util.stream.Stream; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
@ServiceDefinition(quantifier = Quantifier.MULTIPLE, batch = true) | ||
public interface HashAlgorithm { | ||
|
||
@ServiceId(pattern = ServiceId.SCREAMING_KEBAB_CASE) | ||
String getName(); | ||
|
||
String hashToHex(byte[] input); | ||
|
||
static void main(String[] args) { | ||
HashAlgorithmLoader.load().stream() | ||
.filter(algo -> algo.getName().equals("SHA-256")) | ||
.findFirst() | ||
.ifPresent(algo -> System.out.println(algo.hashToHex("hello".getBytes(UTF_8)))); | ||
} | ||
|
||
@ServiceProvider | ||
class MessageDigestBridge implements HashAlgorithmBatch { | ||
|
||
@Override | ||
public Stream<HashAlgorithm> getProviders() { | ||
return Security.getAlgorithms("MessageDigest") | ||
.stream() | ||
.map(MessageDigestAdapter::new); | ||
} | ||
} | ||
|
||
class MessageDigestAdapter implements HashAlgorithm { | ||
|
||
private final String name; | ||
|
||
public MessageDigestAdapter(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String hashToHex(byte[] input) { | ||
return bytesToHex(getThreadUnsafeInstance().digest(input)); | ||
} | ||
|
||
private MessageDigest getThreadUnsafeInstance() { | ||
try { | ||
return MessageDigest.getInstance(name); | ||
} catch (NoSuchAlgorithmException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
private static String bytesToHex(byte[] hash) { | ||
return String.format(Locale.ROOT, "%0" + (hash.length << 1) + "x", new BigInteger(1, hash)); | ||
} | ||
} | ||
} |
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
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
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
File renamed without changes.
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
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
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
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
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