001package var.mom.jms.tasks; 002 003import java.security.MessageDigest; 004import java.security.NoSuchAlgorithmException; 005import java.util.Arrays; 006 007import javax.jms.Connection; 008import javax.jms.ConnectionFactory; 009import javax.jms.JMSException; 010import javax.jms.Message; 011import javax.jms.MessageConsumer; 012import javax.jms.MessageListener; 013import javax.jms.Queue; 014import javax.jms.Session; 015import javax.jms.TextMessage; 016import javax.jms.Topic; 017import javax.naming.Context; 018import javax.naming.InitialContext; 019import javax.naming.NamingException; 020import javax.xml.bind.DatatypeConverter; 021 022public class PasswordCandidateValidator implements MessageListener { 023 private static int messageCounter = 0; 024 private final byte[] target; 025 private final MessageDigest dig; 026 private Connection connection; 027 private Session session; 028 private MessageConsumer taskConsumer; 029 private MessageConsumer successConsumer; 030 private boolean stopped = false; 031 032 public PasswordCandidateValidator(String target) throws NamingException, JMSException, NoSuchAlgorithmException { 033 this.dig = MessageDigest.getInstance("SHA-256"); 034 this.target = DatatypeConverter.parseHexBinary(target); 035 Context ctx = new InitialContext(); 036 ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); 037 Queue queue = (Queue) ctx.lookup(Conf.TASK_QUEUE); 038 this.connection = factory.createConnection(); 039 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 040 this.taskConsumer = this.session.createConsumer(queue); 041 this.taskConsumer.setMessageListener(this); 042 Topic successTopic = (Topic) ctx.lookup(Conf.SUCCESS_TOPIC); 043 this.successConsumer = this.session.createConsumer(successTopic); 044 this.successConsumer.setMessageListener(new MessageListener() { 045 @Override 046 public void onMessage(Message message) { 047 PasswordCandidateValidator.this.stopped = true; 048 System.out.println("Password gefunden. PasswordCandidateValidator wird gestoppt."); 049 050 } 051 }); 052 this.connection.start(); 053 } 054 055 @Override 056 public void onMessage(Message message) { 057 synchronized (PasswordCandidateValidator.class) { 058 if (PasswordCandidateValidator.messageCounter++ >= 99) { 059 System.out.println(System.currentTimeMillis()); 060 PasswordCandidateValidator.messageCounter = 0; 061 } 062 } 063 if (message instanceof TextMessage) { 064 TextMessage task = (TextMessage) message; 065 try { 066 byte[] hash = this.dig.digest(task.getText().getBytes()); 067 if (Arrays.equals(hash, this.target)) { 068 System.out.println("Lòˆsung gefunden: " + task.getText()); 069 } 070 } catch (JMSException e) { 071 System.err.println(e); 072 } 073 } 074 } 075 076 public static void main(String[] args) { 077 PasswordCandidateValidator node = null; 078 try { 079 node = new PasswordCandidateValidator("865ae56c298c677e9d4987fe13268fa915bd041646526c595d293d5448481443"); 080 while (!node.stopped) { 081 Thread.sleep(1000); 082 } 083 } catch (InterruptedException | NamingException | JMSException | NoSuchAlgorithmException e) { 084 System.err.println(e); 085 } finally { 086 try { 087 if ((node != null) && (node.taskConsumer != null)) { 088 node.taskConsumer.close(); 089 } 090 if ((node != null) && (node.session != null)) { 091 node.session.close(); 092 } 093 if ((node != null) && (node.connection != null)) { 094 node.connection.close(); 095 } 096 } catch (JMSException e) { 097 System.err.println(e); 098 } 099 } 100 } 101}