package io.vertx.ext.auth;
import java.util.HashMap;
import java.util.Map;
public final class HashString {
private String id;
private Map<String, String> params;
private String salt;
private String hash;
public HashString(String id, Map<String, String> params, String salt) {
this.id = id;
this.params = params;
this.salt = salt;
}
public HashString(String encoded) {
String[] parts;
if (encoded.length() > 1 && encoded.charAt(0) != '$') {
encoded = encoded.replaceAll("\\{", "\\$\\{");
encoded = encoded.replaceAll("\\}", "\\}\\$");
if (encoded.length() > 1 && encoded.charAt(0) != '$') {
encoded = "$$" + encoded;
}
}
parts = encoded.split("\\$");
if (parts.length < 2) {
throw new IllegalStateException("Not enough segments: " + encoded);
}
switch (parts.length) {
case 2:
id = parts[1];
case 3:
id = parts[1];
hash = parts[2];
break;
case 4:
id = parts[1];
salt = parts[2];
hash = parts[3];
break;
case 5:
default:
id = parts[1];
params = new HashMap<>();
for (String kv : parts[2].split(",")) {
int eq = kv.indexOf('=');
if (eq > 0) {
params.put(kv.substring(0, eq), kv.substring(eq + 1));
}
}
salt = parts[3];
hash = parts[4];
break;
}
}
public String id() {
return id;
}
public String param(String param) {
if (params == null) {
return null;
}
return params.get(param);
}
public Map<String, String> params() {
return params;
}
public String salt() {
return salt;
}
public String hash() {
return hash;
}
public static String encode(HashingAlgorithm algorithm, Map<String, String> params, String salt, String hash) {
StringBuilder sb = new StringBuilder();
if (algorithm.needsSeparator()) {
sb.append('$');
}
sb.append(algorithm.id());
if (params != null) {
if (algorithm.needsSeparator()) {
sb.append('$');
}
boolean notEmpty = false;
for (String key : algorithm.params()) {
String value = params.get(key);
if (value != null) {
if (notEmpty) {
sb.append(',');
}
sb.append(key);
sb.append('=');
sb.append(params.get(key));
notEmpty = true;
}
}
}
if (salt != null) {
if (algorithm.needsSeparator()) {
sb.append('$');
}
sb.append(salt);
}
if (hash != null) {
if (algorithm.needsSeparator()) {
sb.append('$');
}
sb.append(hash);
}
return sb.toString();
}
@Override
public String toString() {
return "id=" + id() + ",params=" + params() + ",salt=" + salt() + ",hash=" + hash();
}
}