-
Notifications
You must be signed in to change notification settings - Fork 10
/
crypto.go
145 lines (122 loc) · 6.89 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// File: ./blockfreight/lib/crypto/crypto.go
// Summary: Application code for Blockfreight™ | The blockchain of global freight.
// License: MIT License
// Company: Blockfreight, Inc.
// Author: Julian Nunez, Neil Tran, Julian Smith, Gian Felipe & contributors
// Site: https://blockfreight.com
// Support: <support@blockfreight.com>
// Copyright © 2017 Blockfreight, Inc. All Rights Reserved.
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// =================================================================================================================================================
// =================================================================================================================================================
//
// BBBBBBBBBBBb lll kkk ffff iii hhh ttt
// BBBB``````BBBB lll kkk fff ``` hhh ttt
// BBBB BBBB lll oooooo ccccccc kkk kkkk fffffff rrr rrr eeeee iii gggggg ggg hhh hhhhh tttttttt
// BBBBBBBBBBBB lll ooo oooo ccc ccc kkk kkk fffffff rrrrrrrr eee eeee iii gggg ggggg hhhh hhhh tttttttt
// BBBBBBBBBBBBBB lll ooo ooo ccc kkkkkkk fff rrrr eeeeeeeeeeeee iii gggg ggg hhh hhh ttt
// BBBB BBB lll ooo ooo ccc kkkk kkkk fff rrr eeeeeeeeeeeee iii ggg ggg hhh hhh ttt
// BBBB BBBB lll oooo oooo cccc ccc kkk kkkk fff rrr eee eee iii ggg gggg hhh hhh tttt ....
// BBBBBBBBBBBBB lll oooooooo ccccccc kkk kkkk fff rrr eeeeeeeee iii gggggg ggg hhh hhh ttttt ....
// ggg ggg
// Blockfreight™ | The blockchain of global freight. ggggggggg
//
// =================================================================================================================================================
// =================================================================================================================================================
// Package crypto provides useful functions to sign BF_TX.
package crypto
import (
// =======================
// Golang Standard library
// =======================
"crypto/ecdsa" // Implements the Elliptic Curve Digital Signature Algorithm, as defined in FIPS 186-3.
"crypto/elliptic" // Implements several standard elliptic curves over prime fields.
"crypto/md5" // Implements the MD5 hash algorithm as defined in RFC 1321.
"crypto/rand" // Implements a cryptographically secure pseudorandom number generator.
"hash" // Provides interfaces for hash functions.
"io" // Provides basic interfaces to I/O primitives.
"math/big" // Implements arbitrary-precision arithmetic (big numbers).
"strconv" // Implements conversions to and from string representations of basic data types.
// ======================
// Blockfreight™ packages
// ======================
"github.com/blockfreight/blockfreight-alpha/lib/app/bf_tx" // Defines the Blockfreight™ Transaction (BF_TX) transaction standard and provides some useful functions to work with the BF_TX.
)
// Function Sign_BF_TX has the whole process of signing each BF_TX.
func Sign_BF_TX(bftx bf_tx.BF_TX) (bf_tx.BF_TX, error) {
content, err := bf_tx.BF_TXContent(bftx)
if err != nil {
return bftx, err
}
pubkeyCurve := elliptic.P256() //see http://golang.org/pkg/crypto/elliptic/#P256
privatekey := new(ecdsa.PrivateKey)
privatekey, err = ecdsa.GenerateKey(pubkeyCurve, rand.Reader) // this generates a public & private key pair
if err != nil {
return bftx, err
}
pubkey := privatekey.PublicKey
// Sign ecdsa style
var h hash.Hash
h = md5.New()
r := big.NewInt(0)
s := big.NewInt(0)
io.WriteString(h, content)
signhash := h.Sum(nil)
r, s, err = ecdsa.Sign(rand.Reader, privatekey, signhash)
if err != nil {
return bftx, err
}
signature := r.Bytes()
signature = append(signature, s.Bytes()...)
sign := ""
for i, _ := range signature {
sign += strconv.Itoa(int(signature[i]))
}
// Verification
verifystatus := ecdsa.Verify(&pubkey, signhash, r, s)
//Set Private Key and Sign to BF_TX
bftx.PrivateKey = *privatekey
bftx.Signhash = signhash
bftx.Signature = sign
bftx.Verified = verifystatus
return bftx, nil
}
// =================================================
// Blockfreight™ | The blockchain of global freight.
// =================================================
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBB BBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBB BBBBBBBBBBBBBBB
// BBBBBBB BBBBBBB BBBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBB BBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBBB BBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBBB BBBBBBBBBBBBBB
// BBBBBBB BBBBBBBBB BBB BBBBB
// BBBBBBB BBBB BBBBB
// BBBBBBB BBBBBBB BBBBB
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
// ==================================================
// Blockfreight™ | The blockchain for global freight.
// ==================================================