-
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.
- Loading branch information
Showing
15 changed files
with
255 additions
and
47 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
package io.github.tap30.hiss; | ||
|
||
import at.favre.lib.crypto.bcrypt.BCrypt; | ||
import io.github.tap30.hiss.key.KeyHashGenerator; | ||
import io.github.tap30.hiss.properties.HissProperties; | ||
import io.github.tap30.hiss.properties.HissPropertiesValidator; | ||
|
||
import java.util.logging.Logger; | ||
|
||
public class HissFactory { | ||
|
||
private static final Logger logger = Logger.getLogger(HissFactory.class.getName()); | ||
|
||
/** | ||
* Creates a Hiss instance with provided <code>HissProperties</code>. | ||
* | ||
* @param hissProperties the properties by which hiss will be instantiated; | ||
* {@link io.github.tap30.hiss.properties.HissPropertiesFromEnv} | ||
* or any custom implementation of | ||
* {@link io.github.tap30.hiss.properties.HissProperties} | ||
* can be used. | ||
* @return {@link Hiss} instance. | ||
* @throws IllegalArgumentException if the properties are not valid. | ||
*/ | ||
public static Hiss createHiss(HissProperties hissProperties) { | ||
return new Hiss(hissProperties); | ||
var keyHashGenerator = new KeyHashGenerator(BCrypt.withDefaults(), BCrypt.verifyer()); | ||
new HissPropertiesValidator(keyHashGenerator).validate(hissProperties); | ||
return new Hiss(hissProperties, keyHashGenerator); | ||
} | ||
|
||
} |
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,12 @@ | ||
package io.github.tap30.hiss.key; | ||
|
||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
@Value | ||
@Builder | ||
public class Key { | ||
String id; | ||
byte[] key; | ||
String keyHash; | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/io/github/tap30/hiss/key/KeyHashGenerator.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,58 @@ | ||
package io.github.tap30.hiss.key; | ||
|
||
import at.favre.lib.crypto.bcrypt.BCrypt; | ||
import io.github.tap30.hiss.utils.StringUtils; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
|
||
public class KeyHashGenerator { | ||
|
||
private static final Logger logger = Logger.getLogger(KeyHashGenerator.class.getName()); | ||
private static final Charset CHARSET = StandardCharsets.UTF_8; | ||
|
||
private final BCrypt.Hasher hasher; | ||
private final BCrypt.Verifyer verifyer; | ||
|
||
public KeyHashGenerator(BCrypt.Hasher hasher, BCrypt.Verifyer verifyer) { | ||
this.hasher = hasher; | ||
this.verifyer = verifyer; | ||
} | ||
|
||
public void generateAndLogHashes(Collection<Key> keys) { | ||
var result = new StringBuilder(); | ||
result.append("Keys' Hash:"); | ||
generateHashes(keys).forEach((k, v) -> result.append("\n ").append(k).append(": ").append(v)); | ||
logger.log(Level.INFO, result.toString()); | ||
} | ||
|
||
/** | ||
* @param keys | ||
* @return map of key ID to key hash. | ||
*/ | ||
public Map<String, String> generateHashes(Collection<Key> keys) { | ||
var hashes = new HashMap<String, String>(); | ||
keys.forEach(k -> hashes.put(k.getId(), new String(hasher.hash(12, k.getKey()), CHARSET))); | ||
return hashes; | ||
} | ||
|
||
/** | ||
* @param keys | ||
* @return invalid key IDs. | ||
*/ | ||
public Set<String> validateKeyHashes(Collection<Key> keys) { | ||
return keys.stream() | ||
.filter(key -> StringUtils.hasText(key.getKeyHash())) | ||
.filter(key -> !verifyer.verify(key.getKey(), key.getKeyHash().getBytes(CHARSET)).verified) | ||
.map(Key::getId) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.