OpenCores
URL https://opencores.org/ocsvn/fpga-cf/fpga-cf/trunk

Subversion Repositories fpga-cf

[/] [fpga-cf/] [trunk/] [java/] [src/] [edu/] [byu/] [cc/] [plieber/] [fpgaenet/] [modmod/] [SHA1Widget.java] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 peteralieb
package edu.byu.cc.plieber.fpgaenet.modmod;
2 7 peteralieb
import java.io.IOException;
3
import java.net.InetAddress;
4 2 peteralieb
import java.util.ArrayList;
5
 
6
import com.trolltech.qt.gui.*;
7
import com.trolltech.qt.gui.QSizePolicy.Policy;
8
 
9
import edu.byu.cc.plieber.fpgaenet.fcp.FCPException;
10
import edu.byu.cc.plieber.fpgaenet.fcp.FCPProtocol;
11
import edu.byu.cc.plieber.util.StringUtil;
12
 
13
public class SHA1Widget extends ModuleControlWidget{
14 7 peteralieb
 
15
        public static void main(String[] args) {
16
                QApplication.initialize(args);
17
 
18
                FCPProtocol p = null;
19
                try {
20
                        p = new FCPProtocol();
21
                        p.connect(InetAddress.getByName("10.0.1.42"));
22
                        while (!p.isConnected());
23
                } catch (IOException e) {
24
                        // TODO Auto-generated catch block
25
                        e.printStackTrace();
26
                }
27
                SHA1Widget w = new SHA1Widget(null, p);
28
                w.setChannelNumber(6);
29
                w.show();
30
                QApplication.exec();
31
                p.disconnect();
32
 
33
        }
34 2 peteralieb
 
35
    public SHA1Widget(QWidget parent, FCPProtocol protocol){
36
        super(parent);
37
        fcpProtocol = protocol;
38
        createWidgets();
39
        connectSignalsAndSlots();
40
    }
41
 
42
    private QVBoxLayout mainLayout;
43
    private QHBoxLayout formatLayout;
44
 
45
    private QLabel lblTitle;
46
 
47
    private QRadioButton radASCII;
48
    private QRadioButton radHex;
49
 
50
    private QLineEdit txtRead;
51
    private QPushButton btnCalculate;
52
    private QLineEdit txtResult;
53
 
54
    FCPProtocol fcpProtocol;
55
 
56
        private void createWidgets() {
57
                mainLayout = new QVBoxLayout(this);
58
                this.setLayout(mainLayout);
59
                lblTitle = new QLabel("SHA1 Calculator");
60
                mainLayout.addWidget(lblTitle);
61
                formatLayout = new QHBoxLayout();
62
                mainLayout.addLayout(formatLayout);
63
                radASCII = new QRadioButton("ASCII");
64
                radASCII.setChecked(true);
65
                radHex = new QRadioButton("Hex");
66
                formatLayout.addWidget(radASCII);
67
                formatLayout.addWidget(radHex);
68
                formatLayout.addStretch();
69
                txtRead = new QLineEdit();
70
                btnCalculate = new QPushButton("Calculate");
71
                btnCalculate.setSizePolicy(new QSizePolicy(Policy.Maximum, Policy.Maximum));
72
                txtResult = new QLineEdit();
73
                mainLayout.addWidget(txtRead);
74
                mainLayout.addWidget(btnCalculate);
75
                mainLayout.addWidget(txtResult);
76
                mainLayout.addStretch();
77
        }
78
 
79
        private void connectSignalsAndSlots() {
80
                btnCalculate.clicked.connect(this, "calculate()");
81
        }
82
 
83
        @Override
84
        public void setChannelNumber(int channelNumber) {
85
                this.channelNumber = channelNumber;
86
                lblTitle.setText("SHA1 Calculator (Channel " + channelNumber + ")");
87
        }
88
 
89
        @SuppressWarnings("unused")
90
        private void calculate() {
91
                String text = txtRead.text();
92
                ArrayList<Byte> pass;
93
                long len = 0;
94
                if (radASCII.isChecked()) {
95
                        pass = StringUtil.stringToByteList(text);
96
                        len = text.length();
97
                }
98
                else {
99
                        String[] tokens = text.split(" ");
100
                        pass = new ArrayList<Byte>();
101
                        for (String bytestr : tokens) {
102
                                pass.add((byte) Integer.parseInt(bytestr, 16));
103
                        }
104
                        len = pass.size();
105
                }
106
                pass.add(new Byte((byte)0x80));
107
                while (pass.size()*8 % 512 != 448) {
108
                        pass.add(new Byte((byte)0x00));
109
                }
110
                len = len * 8;
111
                pass.add(new Byte((byte)((len >> 56) & 0xff)));
112
                pass.add(new Byte((byte)((len >> 48) & 0xff)));
113
                pass.add(new Byte((byte)((len >> 40) & 0xff)));
114
                pass.add(new Byte((byte)((len >> 32) & 0xff)));
115
                pass.add(new Byte((byte)((len >> 24) & 0xff)));
116
                pass.add(new Byte((byte)((len >> 16) & 0xff)));
117
                pass.add(new Byte((byte)((len >> 8) & 0xff)));
118
                pass.add(new Byte((byte)((len) & 0xff)));
119
                try {
120
                        fcpProtocol.send(channelNumber, pass, pass.size());
121
                        fcpProtocol.sendDataRequest(channelNumber, 20);
122
                } catch (FCPException e) {
123
                        // TODO Auto-generated catch block
124
                        e.printStackTrace();
125
                }
126
                byte[] bytes = fcpProtocol.getDataResponse();
127
                txtResult.clear();
128
                txtResult.setText(StringUtil.arrayToHexString(bytes));
129
        }
130
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.