diff --git a/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Routes.java b/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Routes.java index 9d8c9d5..67d94e8 100644 --- a/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Routes.java +++ b/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Routes.java @@ -8,9 +8,12 @@ import java.net.http.HttpResponse; import java.security.*; import java.security.spec.InvalidKeySpecException; -import java.util.Arrays; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Base64; +import java.util.Date; import java.util.Map; +import java.util.TimeZone; import com.fasterxml.jackson.databind.JsonNode; import org.json.JSONException; @@ -42,13 +45,26 @@ public class Routes extends Utils{ @Autowired private String gatewayUrl; - private Logger logger = LoggerFactory.getLogger(Routes.class);; + private final Logger logger = LoggerFactory.getLogger(Routes.class);; @GetMapping("/get-keys") public ResponseEntity> getKeys (){ return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(keys); } + @PostMapping("/create-header") + public + String createHeader(@RequestBody JsonNode req) throws Exception { + long created = System.currentTimeMillis() / 1000L; + long expires = created + 300000; + String hashedReq = hashMassage(req.get("value").toString(),created,expires); + String signature = sign(Base64.getDecoder().decode(req.get("private_key").asText()),hashedReq.getBytes()); + String subscriberId = "altiux.com"; + String uniqueKeyId = "c9aa1b41-04e9-43e2-bd89-9ddcdecbf4cf"; + + return "Signature keyId=\"" + subscriberId + "|" + uniqueKeyId + "|" + "ed25519\"" + ",algorithm=\"ed25519\"," + "created=\"" + created + "\",expires=\"" + expires + "\",headers=\"(created) (expires)" + " digest\",signature=\"" + signature + "\""; + } + @PostMapping("/subscribe") public ResponseEntity subscribe(@RequestBody JsonNode subscribeBody) throws NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, JSONException, IOException, InterruptedException { diff --git a/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Utils.java b/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Utils.java index 063a9e7..ac3083d 100644 --- a/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Utils.java +++ b/utilities/on_subscibe-service/java/src/main/java/ondc/onboarding/utility/Utils.java @@ -5,6 +5,7 @@ import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; import java.security.*; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; @@ -33,17 +34,34 @@ public static CryptoKeyPair generateEncDecKey() throws InvalidKeyException, NoSu return new CryptoKeyPair(kp.getPublic().getEncoded(),kp.getPrivate().getEncoded()); } - public static String fromBase64(byte[] src){ + public static String toBase64(byte[] src){ return Base64.getEncoder().encodeToString(src); } + public static String hashMassage(String req, long created,long expires) throws Exception { + byte[] digest = generateBlakeHash(req); + return """ + (created): %s + (expires): %s + digest: BLAKE-512=%s""".formatted(created,expires, toBase64(digest)); + } + public static byte[] generateBlakeHash(String req) throws Exception { + if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { + Security.addProvider(new BouncyCastleProvider()); + } + MessageDigest digest = MessageDigest.getInstance("BLAKE2B-512", BouncyCastleProvider.PROVIDER_NAME); + digest.reset(); + digest.update(req.getBytes(StandardCharsets.UTF_8)); + return digest.digest(); + } + public static String sign(byte[] privateKey,byte[] message) { // initialise signature variable byte[] signature = new byte[Ed25519.SIGNATURE_SIZE]; // sign the received message with given private key Ed25519.sign(privateKey, 0, message, 0, message.length, signature, 0); - return fromBase64(signature); + return toBase64(signature); } public static byte[] encryptDecrypt(int mode, byte[] challenge_string,byte[] privateKey, byte[] publicKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {