Pārlūkot izejas kodu

added signature methods

yauhenip 6 gadus atpakaļ
vecāks
revīzija
30f5b96216

+ 101
- 11
.idea/workspace.xml Parādīt failu

@@ -2,8 +2,10 @@
2 2
 <project version="4">
3 3
   <component name="ChangeListManager">
4 4
     <list default="true" id="15cb9d44-2114-4e87-9e35-a3d14b1a4bbd" name="Default" comment="">
5
-      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
6
-      <change beforePath="$PROJECT_DIR$/src/main/java/com/buecoin01/model/Wallet.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/buecoin01/model/Wallet.java" afterDir="false" />
5
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/buecoin01/utility/TransactionInput.java" afterDir="false" />
6
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/buecoin01/utility/TransactionOutput.java" afterDir="false" />
7
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/buecoin01/model/Transaction.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/buecoin01/model/Transaction.java" afterDir="false" />
8
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/buecoin01/utility/Encryption.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/buecoin01/utility/Encryption.java" afterDir="false" />
7 9
     </list>
8 10
     <ignored path="$PROJECT_DIR$/target/" />
9 11
     <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@@ -15,11 +17,11 @@
15 17
   </component>
16 18
   <component name="FileEditorManager">
17 19
     <leaf>
18
-      <file leaf-file-name="Wallet.java" pinned="false" current-in-tab="true">
20
+      <file leaf-file-name="Wallet.java" pinned="false" current-in-tab="false">
19 21
         <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/model/Wallet.java">
20 22
           <provider selected="true" editor-type-id="text-editor">
21
-            <state relative-caret-position="446">
22
-              <caret line="68" selection-start-line="68" selection-end-line="68" />
23
+            <state relative-caret-position="105">
24
+              <caret line="7" column="13" selection-start-line="7" selection-start-column="13" selection-end-line="7" selection-end-column="13" />
23 25
               <folding>
24 26
                 <element signature="imports" expanded="true" />
25 27
                 <element signature="e#549#550#0" expanded="true" />
@@ -29,6 +31,49 @@
29 31
           </provider>
30 32
         </entry>
31 33
       </file>
34
+      <file leaf-file-name="Transaction.java" pinned="false" current-in-tab="false">
35
+        <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/model/Transaction.java">
36
+          <provider selected="true" editor-type-id="text-editor">
37
+            <state relative-caret-position="270">
38
+              <caret line="18" column="40" selection-start-line="18" selection-start-column="40" selection-end-line="18" selection-end-column="40" />
39
+              <folding>
40
+                <element signature="imports" expanded="true" />
41
+              </folding>
42
+            </state>
43
+          </provider>
44
+        </entry>
45
+      </file>
46
+      <file leaf-file-name="TransactionInput.java" pinned="false" current-in-tab="false">
47
+        <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/utility/TransactionInput.java">
48
+          <provider selected="true" editor-type-id="text-editor">
49
+            <state relative-caret-position="30">
50
+              <caret line="2" column="29" selection-start-line="2" selection-start-column="29" selection-end-line="2" selection-end-column="29" />
51
+            </state>
52
+          </provider>
53
+        </entry>
54
+      </file>
55
+      <file leaf-file-name="TransactionOutput.java" pinned="false" current-in-tab="false">
56
+        <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/utility/TransactionOutput.java">
57
+          <provider selected="true" editor-type-id="text-editor">
58
+            <state relative-caret-position="30">
59
+              <caret line="2" column="30" selection-start-line="2" selection-start-column="30" selection-end-line="2" selection-end-column="30" />
60
+            </state>
61
+          </provider>
62
+        </entry>
63
+      </file>
64
+      <file leaf-file-name="Encryption.java" pinned="false" current-in-tab="true">
65
+        <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/utility/Encryption.java">
66
+          <provider selected="true" editor-type-id="text-editor">
67
+            <state relative-caret-position="223">
68
+              <caret line="30" column="32" lean-forward="true" selection-start-line="30" selection-start-column="32" selection-end-line="30" selection-end-column="32" />
69
+              <folding>
70
+                <element signature="e#1683#1684#0" expanded="true" />
71
+                <element signature="e#1724#1725#0" expanded="true" />
72
+              </folding>
73
+            </state>
74
+          </provider>
75
+        </entry>
76
+      </file>
32 77
       <file leaf-file-name="Blockchain.java" pinned="false" current-in-tab="false">
33 78
         <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/model/Blockchain.java">
34 79
           <provider selected="true" editor-type-id="text-editor">
@@ -40,6 +85,13 @@
40 85
       </file>
41 86
     </leaf>
42 87
   </component>
88
+  <component name="FileTemplateManagerImpl">
89
+    <option name="RECENT_TEMPLATES">
90
+      <list>
91
+        <option value="Class" />
92
+      </list>
93
+    </option>
94
+  </component>
43 95
   <component name="Git.Settings">
44 96
     <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
45 97
   </component>
@@ -54,6 +106,8 @@
54 106
     <option name="CHANGED_PATHS">
55 107
       <list>
56 108
         <option value="$PROJECT_DIR$/src/main/java/com/buecoin01/model/Wallet.java" />
109
+        <option value="$PROJECT_DIR$/src/main/java/com/buecoin01/utility/Encryption.java" />
110
+        <option value="$PROJECT_DIR$/src/main/java/com/buecoin01/model/Transaction.java" />
57 111
       </list>
58 112
     </option>
59 113
   </component>
@@ -219,15 +273,16 @@
219 273
       <option name="number" value="Default" />
220 274
       <option name="presentableId" value="Default" />
221 275
       <updated>1532353115597</updated>
222
-      <workItem from="1532353121145" duration="2380000" />
276
+      <workItem from="1532353121145" duration="5067000" />
223 277
     </task>
224 278
     <servers />
225 279
   </component>
226 280
   <component name="TimeTrackingManager">
227
-    <option name="totallyTimeSpent" value="2380000" />
281
+    <option name="totallyTimeSpent" value="5067000" />
228 282
   </component>
229 283
   <component name="ToolWindowManager">
230 284
     <frame x="-57" y="-1047" width="1832" height="903" extended-state="0" />
285
+    <editor active="true" />
231 286
     <layout>
232 287
       <window_info anchor="right" id="Palette" />
233 288
       <window_info anchor="bottom" id="Event Log" side_tool="true" />
@@ -248,8 +303,8 @@
248 303
       <window_info anchor="bottom" id="Run" order="2" />
249 304
       <window_info anchor="bottom" id="Version Control" />
250 305
       <window_info anchor="bottom" id="Spring" />
251
-      <window_info active="true" anchor="bottom" id="Terminal" visible="true" weight="0.3287841" />
252
-      <window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.19497207" />
306
+      <window_info anchor="bottom" id="Terminal" visible="true" weight="0.3287841" />
307
+      <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.19497207" />
253 308
       <window_info anchor="right" id="Bean Validation" />
254 309
       <window_info id="Web" side_tool="true" />
255 310
       <window_info anchor="right" id="Theme Preview" />
@@ -297,10 +352,24 @@
297 352
         </state>
298 353
       </provider>
299 354
     </entry>
355
+    <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/utility/TransactionInput.java">
356
+      <provider selected="true" editor-type-id="text-editor">
357
+        <state relative-caret-position="30">
358
+          <caret line="2" column="29" selection-start-line="2" selection-start-column="29" selection-end-line="2" selection-end-column="29" />
359
+        </state>
360
+      </provider>
361
+    </entry>
362
+    <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/utility/TransactionOutput.java">
363
+      <provider selected="true" editor-type-id="text-editor">
364
+        <state relative-caret-position="30">
365
+          <caret line="2" column="30" selection-start-line="2" selection-start-column="30" selection-end-line="2" selection-end-column="30" />
366
+        </state>
367
+      </provider>
368
+    </entry>
300 369
     <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/model/Wallet.java">
301 370
       <provider selected="true" editor-type-id="text-editor">
302
-        <state relative-caret-position="446">
303
-          <caret line="68" selection-start-line="68" selection-end-line="68" />
371
+        <state relative-caret-position="105">
372
+          <caret line="7" column="13" selection-start-line="7" selection-start-column="13" selection-end-line="7" selection-end-column="13" />
304 373
           <folding>
305 374
             <element signature="imports" expanded="true" />
306 375
             <element signature="e#549#550#0" expanded="true" />
@@ -309,6 +378,27 @@
309 378
         </state>
310 379
       </provider>
311 380
     </entry>
381
+    <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/model/Transaction.java">
382
+      <provider selected="true" editor-type-id="text-editor">
383
+        <state relative-caret-position="270">
384
+          <caret line="18" column="40" selection-start-line="18" selection-start-column="40" selection-end-line="18" selection-end-column="40" />
385
+          <folding>
386
+            <element signature="imports" expanded="true" />
387
+          </folding>
388
+        </state>
389
+      </provider>
390
+    </entry>
391
+    <entry file="file://$PROJECT_DIR$/src/main/java/com/buecoin01/utility/Encryption.java">
392
+      <provider selected="true" editor-type-id="text-editor">
393
+        <state relative-caret-position="223">
394
+          <caret line="30" column="32" lean-forward="true" selection-start-line="30" selection-start-column="32" selection-end-line="30" selection-end-column="32" />
395
+          <folding>
396
+            <element signature="e#1683#1684#0" expanded="true" />
397
+            <element signature="e#1724#1725#0" expanded="true" />
398
+          </folding>
399
+        </state>
400
+      </provider>
401
+    </entry>
312 402
   </component>
313 403
   <component name="masterDetails">
314 404
     <states>

+ 3
- 0
src/main/java/com/buecoin01/model/Transaction.java Parādīt failu

@@ -1,9 +1,12 @@
1 1
 package com.buecoin01.model;
2 2
 
3 3
 import com.buecoin01.utility.Encryption;
4
+import com.buecoin01.utility.TransactionInput;
5
+import com.buecoin01.utility.TransactionOutput;
4 6
 
5 7
 import java.security.PublicKey;
6 8
 import java.util.ArrayList;
9
+import java.util.List;
7 10
 
8 11
 public class Transaction {
9 12
     private String transactionId; //hash of the transaction

+ 38
- 0
src/main/java/com/buecoin01/utility/Encryption.java Parādīt failu

@@ -3,6 +3,11 @@ package com.buecoin01.utility;
3 3
 import com.google.common.hash.Hashing;
4 4
 
5 5
 import java.nio.charset.StandardCharsets;
6
+import java.security.Key;
7
+import java.security.PrivateKey;
8
+import java.security.PublicKey;
9
+import java.security.Signature;
10
+import java.util.Base64;
6 11
 
7 12
 public class Encryption {
8 13
     public static String applySha256(String input) {
@@ -14,4 +19,37 @@ public class Encryption {
14 19
     public static String createTarget(int difficulty) {
15 20
         return new String(new char[difficulty]).replace('\0', '0');
16 21
     }
22
+
23
+    //Apply ECDSA Signature and return result as bytes
24
+    public static byte[] applyECDSASig(PrivateKey privateKey, String input) {
25
+        Signature dsa;
26
+        byte[] realSig = new byte[0];
27
+        try {
28
+            dsa = Signature.getInstance("ECDSA","BC");
29
+            dsa.initSign(privateKey);
30
+            byte[] strByte = input.getBytes();
31
+            dsa.update(strByte);
32
+            realSig = dsa.sign();
33
+        } catch (Exception e) {
34
+            throw new RuntimeException(e);
35
+        }
36
+        return realSig;
37
+    }
38
+
39
+    //Verify a String Signature
40
+    public static boolean verifyECDSASig(PublicKey publicKey, String data, byte[] signature) {
41
+        try {
42
+            Signature ecdsaVerify = Signature.getInstance("ECDSA", "BC");
43
+            ecdsaVerify.initVerify(publicKey);
44
+            ecdsaVerify.update(data.getBytes());
45
+            return  ecdsaVerify.verify(signature);
46
+        } catch (Exception e) {
47
+            throw new RuntimeException(e);
48
+        }
49
+    }
50
+
51
+    //Return encoded string from a key
52
+    public static String getStringFromKey(Key key) {
53
+        return Base64.getEncoder().encodeToString(key.getEncoded());
54
+    }
17 55
 }

+ 4
- 0
src/main/java/com/buecoin01/utility/TransactionInput.java Parādīt failu

@@ -0,0 +1,4 @@
1
+package com.buecoin01.utility;
2
+
3
+public class TransactionInput {
4
+}

+ 4
- 0
src/main/java/com/buecoin01/utility/TransactionOutput.java Parādīt failu

@@ -0,0 +1,4 @@
1
+package com.buecoin01.utility;
2
+
3
+public class TransactionOutput {
4
+}