1

I am trying to connect to the code I have wrote through my browser, but unfortunately can not figure out how to do so. I have tried 127.0.0.1/hash but it did not work even though I have build the project using maven build compile. was wondering if someone could tell me what am I doing wrong here.

here is the code :

package com.snhu.sslserver;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets; 

@SpringBootApplication

public class ServerApplication {

public static void main(String[] args) {

SpringApplication.run(ServerApplication.class, args);

}

}

@RestController

class ServerController{

 public static String calculateHash(String name)  throws NoSuchAlgorithmException 
    {  
        MessageDigest md = MessageDigest.getInstance("SHA-256");  
        byte[] hash =  md.digest(name.getBytes(StandardCharsets.UTF_8));
        BigInteger number = new BigInteger(1, hash);  
        StringBuilder hexString = new StringBuilder(number.toString(16));  
        while (hexString.length() < 32)  
        {  
            hexString.insert(0, '0');  
        }  
        return hexString.toString();  
     
    } 
@RequestMapping("/hash")
public String myHash() throws NoSuchAlgorithmException{

String data = "Hello Kamran Khosravi!";
String hash = calculateHash(data);

return "<p>data:"+data+" : SHA-256 "+" : "+hash;

}

}
Kamran
  • 111
  • 1
  • 8
  • 1
    127.0.0.1/hash is incorrect. You need port too. 127.0.0.1:8080/hash –  Apr 02 '22 at 15:46
  • I have tried to connect to 127.0.0.1:8080/hash as well with no luck. says the site can't be reached. – Kamran Apr 02 '22 at 16:02
  • post your application.yml/.properties – Missael De Nadai Apr 02 '22 at 16:08
  • server.port=8443 server.ssl.key-alias=tomcat server.ssl.key-store-password=snhu4321 server.ssl.key-store=keystore.p12 server.ssl.key-store-type=PKCS12 – Kamran Apr 02 '22 at 16:10
  • 1
    127.0.0.1:8443/hash –  Apr 02 '22 at 16:10
  • I just copied your code. Works perfectly fine. Your error was with port number, which is assigned as 8443 in your settings, so you need 127.0.0.1:8443/hash –  Apr 02 '22 at 16:14
  • this might seem silly but I am pretty new to programming. how do I compile it properly? currently I am using Maven build and set the goal as compile. it runs successfully but when I type in the 127.0.0.1:8443/hash in the chrome browser it wont connect. – Kamran Apr 02 '22 at 16:27
  • Are you using Eclipse? –  Apr 02 '22 at 17:18
  • yes freddy i am using Eclipse. – Kamran Apr 02 '22 at 20:14
  • 1
    https://stackoverflow.com/questions/23677808/how-to-run-spring-boot-web-application-in-eclipse-itself –  Apr 03 '22 at 03:39

1 Answers1

0

you can just replace the port 8080 with respective to your port number which you have in application.properties

Command : spring-boot:run

http://localhost:8080/hash