Skip to content
Snippets Groups Projects
Commit 95cb8e14 authored by jbuechele's avatar jbuechele
Browse files

added basic hashing service implementation

parent 0dbeb9d8
No related branches found
No related tags found
No related merge requests found
package eu.fairkom.faircommons.hashing_service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HashingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HashingServiceApplication.class, args);
}
}
package eu.fairkom.faircommons.hashing_service.config;
import eu.fairkom.faircommons.common.rabbitmq.RabbitMqConfiguration;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackageClasses = RabbitMqConfiguration.class)
public class HashingServiceConfiguration {
}
package eu.fairkom.faircommons.hashing_service.service;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
@Service
public class HashingService {
public String getChecksum(InputStream inputStream, String algorithm) {
try {
var messageDigest = MessageDigest.getInstance("SHA-256");
try (InputStream is = inputStream) {
var digestInputStream = new DigestInputStream(is, messageDigest);
while (digestInputStream.read() != -1) ; //empty loop to clear the data
messageDigest = digestInputStream.getMessageDigest();
}
return bytesToHex(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
package eu.fairkom.faircommons.hashing_service.service;
import eu.fairkom.faircommons.common.rabbitmq.RabbitMqReceiver;
import eu.fairkom.faircommons.common.rabbitmq.message.HashingMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import static eu.fairkom.faircommons.common.rabbitmq.RabbitMqConfiguration.HASHING_QUEUE;
@Component
@RabbitListener(queues = HASHING_QUEUE)
public class HashingServiceReceiver implements RabbitMqReceiver<HashingMessage> {
private final HashingService hashingService;
public HashingServiceReceiver(HashingService hashingService) {
this.hashingService = hashingService;
}
private final Logger logger = LoggerFactory.getLogger(HashingServiceReceiver.class);
@Override
public void receive(HashingMessage message) {
logger.info("Message received: " + message.toString());
}
}
spring:
rabbitmq:
host: localhost
username: user
password: password
port: 5672
\ No newline at end of file
spring:
application:
name: hashing-service
server:
port: 8081
servlet:
context-path: /${spring.application.name}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment