-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrypto.js
More file actions
193 lines (165 loc) · 5.89 KB
/
Crypto.js
File metadata and controls
193 lines (165 loc) · 5.89 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2017-Present The Kunta Protocol Authors
This file is part of the Kunta Protocol library.
The Kunta Protocol is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Kunta Protocol is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Kunta Protocol library. If not, see <http://www.gnu.org/licenses/>.
*/
var CryptoJS = require("crypto-js");
const crypto2 = require('crypto2');
var LOGGER = require('./Logger.js')
var CRYTPOJSSHA256 = require("crypto-js/sha256");
var fs = require('fs')
const KLOC = "core/storage/KEYS/"
const PRIVATEKEY = "PRIVATE.kk"
const PUBLICKEY = "PUBLIC.kk"
var saveKeys = (privateKey, publicKey) => {
try{
fs.writeFileSync(KLOC+PRIVATEKEY, privateKey, 'utf-8')
fs.writeFileSync(KLOC+PUBLICKEY, publicKey, 'utf-8')
}catch(e){
console.log("saveKeys error")
}
}
var CRYPTOOBJ;
var privkeypem;
var pubkeypem;
var SOLVED_BY_ANOTHER = false
var MineAttempt = (index, previousHash, timestamp, data, rootInstances, nonce) => {
var contentForHash = (index + previousHash + timestamp + data + rootInstances + nonce)
var SHA256HashofBlock = SHA256(contentForHash)
return SHA256HashofBlock;
};
var CalculateHash = (index, previousHash, timestamp, data, rootInstances, nonce) => {
var contentForHash = (index + previousHash + timestamp + data + rootInstances + nonce)
var SHA256HashofBlock = SHA256(contentForHash)
LOGGER("Result from 'SHA256' hash: "+SHA256HashofBlock, "SHA256")
return SHA256HashofBlock;
};
var CalculateHashForBlock = (block) => {
LOGGER(JSON.stringify(block), "CALCULATEHASHFORBLOCK")
console.log("CALCULATEHASHFORBLOCK:")
console.log(block)
var RIOBJS = []
block.body.rootInstances.forEach(ri => {
console.log("block.body.rootInstances.forEach")
console.log(ri)
try{
RIOBJS.push(JSON.parse(ri))
}catch(e){
RIOBJS.push(ri)
}
})
console.log("RIOBJS")
console.log(block.body.rootInstances)
console.log(RIOBJS)
console.log(JSON.stringify(RIOBJS))
var hashResult = CalculateHash(block.header.blockIndex,
block.header.previousHash,
block.header.timestamp,
JSON.stringify(block.body.data),
JSON.stringify(RIOBJS),
block.header.nonce)
return hashResult;
};
var HASHDATA = (content) => {
return SHA256(content)
}
var SHA256 = (content) => {
return CRYTPOJSSHA256(content).toString()
}
var BASE64ENCODE = (content) => {
return Buffer.from(content).toString('base64')
}
var BASE64DECODE = (content) => {
return Buffer.from(content, 'base64').toString('ascii')
}
class Crypto{
constructor() {
this.publicKeyPem = pubkeypem;
this.privateKeyPem = privkeypem;
this.privkey = "PRIVKEY"
this.pubkey = "PUBKEY"
this.pubkeyHash = SHA256(this.pubkey)
this.digitalSignature = CryptoJS.SHA256(this.publicKey+this.privateKey).toString();
}
}
CRYPTOOBJ = new Crypto()
var getKeys = async (cb) => {
try{
console.log("inside getkeys")
//var readPrivateKey = fs.readFileSync(KLOC+PRIVATEKEY).toString()
//var readPublicKey = fs.readFileSync(KLOC+PUBLICKEY).toString()
///////////TODO: IF KEYS EXIST:
const privateKey = await crypto2.readPrivateKey(KLOC+PRIVATEKEY);
const publicKey = await crypto2.readPublicKey(KLOC+PUBLICKEY);
//TODO: if not
//const { privateKey, publicKey } = await crypto2.createKeyPair();
//saveKeys(privateKey, publicKey)
cb({
private: privateKey,
public: publicKey
})
}catch(err){
///keys prob don't exist, CREATE THEM
console.log("keys error: "+err)
var privateKey = "PRIVATEKEYPEM"//key.toPrivatePem().toString('ascii');
var publicKey = "PUBKEYPEM"//key.toPublicPem().toString('ascii');
//saveKeys(privateKey, publicKey)
/////////////TODO: if not
//const { privateKey, publicKey } = await crypto2.createKeyPair();
//saveKeys(privateKey, publicKey)
cb({
private: privateKey,
public: publicKey
})
}
}
//TODO: ONLY CALL GETKEYS WHEN ON START
getKeys(async function(keysObj){
LOGGER(keysObj, "keysObj")
privkeypem = keysObj.private;
pubkeypem = keysObj.public;
console.log(keysObj)
console.log("3")
/////////////////////// SIGN EXAMPLE
// const signature = await crypto2.sign.sha256('the native web',
// keysObj.private);
// LOGGER(signature, "getKeys:signature")
// const isSignatureValid = await crypto2.verify.sha256('the native web',
// keysObj.public,
// signature);
// LOGGER(isSignatureValid, "isSignatureValid")
// LOGGER(BASE64ENCODE(privkeypem), "BASE64ENCODE(privkeypem)")
// LOGGER(BASE64ENCODE(pubkeypem), "BASE64ENCODE(pubkeypem)")
}).catch(err => {
console.log(err)
})
var getSolvedByAnother = () => {
return SOLVED_BY_ANOTHER
}
var setSolvedByAnother = (sba) => {
SOLVED_BY_ANOTHER = sba
}
module.exports = {
Crypto,
MineAttempt,
CalculateHashForBlock,
CalculateHash,
CRYPTOOBJ,
CryptoJS,
SHA256,
BASE64ENCODE,
BASE64DECODE,
HASHDATA,
SOLVED_BY_ANOTHER,
getSolvedByAnother,
setSolvedByAnother
}