forked from infosave2007/phpblockchain
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto-cli.php
More file actions
Latest commit
executable file
·162 lines (137 loc) · 5.51 KB
/
Copy pathcrypto-cli.php
File metadata and controls
executable file
·162 lines (137 loc) · 5.51 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
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* Crypto Messages CLI Tool
*
* Command line interface for blockchain crypto messaging operations
*/
if (php_sapi_name() !== 'cli') {
die("This script can only be run from command line\n");
}
// Define constants
define('BLOCKCHAIN_VERSION', '2.0.0');
// Autoload
require_once__DIR__ . '/vendor/autoload.php';
useBlockchain\Core\Application;
useBlockchain\Core\Cryptography\MessageEncryption;
// Color output functions
functioncolor($text, $color = null) {
$colors = [
'black' => '0;30',
'dark_gray' => '1;30',
'blue' => '0;34',
'light_blue' => '1;34',
'green' => '0;32',
'light_green' => '1;32',
'cyan' => '0;36',
'light_cyan' => '1;36',
'red' => '0;31',
'light_red' => '1;31',
'purple' => '0;35',
'light_purple' => '1;35',
'brown' => '0;33',
'yellow' => '1;33',
'light_gray' => '0;37',
'white' => '1;37'
];
if (!$color || !isset($colors[$color])) {
return$text;
}
return"\033[" . $colors[$color] . "m" . $text . "\033[0m";
}
functioninfo($text) {
echocolor("[INFO] ", 'green') . $text . "\n";
}
functionerror($text) {
echocolor("[ERROR] ", 'red') . $text . "\n";
}
// Load configuration
$config = [
'database' => [
'host' => 'localhost',
'port' => 3306,
'database' => 'blockchain_test',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'options' => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
],
'blockchain' => [
'network_name' => 'Crypto Messages Network',
'token_symbol' => 'CMN',
'consensus_algorithm' => 'proof_of_stake'
],
'consensus' => [
'pos' => []
]
];
// Handle command line arguments
$command = $argv[1] ?? 'help';
try {
if ($command === 'generate-keys') {
// Special case - generate keys without database connection
echocolor("Generating RSA Key Pair...", 'cyan') . "\n";
$keyPair = MessageEncryption::generateRSAKeyPair(2048);
echocolor("RSA Key Pair Generated Successfully!", 'green') . "\n";
echocolor("=====================================", 'green') . "\n";
echo"Public Key:\n";
echo$keyPair['public_key'] . "\n";
echo"\nPrivate Key:\n";
echo$keyPair['private_key'] . "\n";
echocolor("\nIMPORTANT: Save your private key securely! It cannot be recovered if lost.", 'yellow') . "\n";
echocolor("The public key can be shared with others to receive encrypted messages.", 'yellow') . "\n";
exit(0);
}
if ($command === 'encrypt-test') {
// Test encryption without database
$message = $argv[2] ?? "Hello, this is a test message!";
echocolor("Testing Message Encryption...", 'cyan') . "\n";
// Generate test keys
$keyPair = MessageEncryption::generateRSAKeyPair(1024); // Smaller for demo
echo"Original message: " . color($message, 'yellow') . "\n";
// Test hybrid encryption
$encrypted = MessageEncryption::encryptHybrid($message, $keyPair['public_key']);
echocolor("Message encrypted successfully!", 'green') . "\n";
echo"Encrypted size: " . strlen(json_encode($encrypted)) . " bytes\n";
// Test decryption
$decrypted = MessageEncryption::decryptHybrid($encrypted, $keyPair['private_key']);
echo"Decrypted message: " . color($decrypted, 'green') . "\n";
if ($message === $decrypted) {
echocolor("✓ Encryption/Decryption test PASSED!", 'green') . "\n";
} else {
echocolor("✗ Encryption/Decryption test FAILED!", 'red') . "\n";
}
exit(0);
}
if ($command === 'help' || $command === '--help' || $command === '-h') {
echocolor("Crypto Messages CLI Tool v" . BLOCKCHAIN_VERSION, 'cyan') . "\n";
echocolor("=====================================", 'cyan') . "\n";
echo"Available commands:\n";
echocolor(" generate-keys", 'light_blue') . " Generate new RSA key pair\n";
echocolor(" encrypt-test [message]", 'light_blue') . " Test encryption/decryption\n";
echocolor(" create-wallet <name>", 'light_blue') . " Create new wallet with keys\n";
echocolor(" send-message <from> <to> <msg> [encrypt]", 'light_blue') . " Send message\n";
echocolor(" read-messages <addr> [priv_key]", 'light_blue') . " Read messages for address\n";
echocolor(" help", 'light_blue') . " Show this help\n";
echo"\nNote: Commands requiring database will need proper database configuration.\n";
exit(0);
}
// For other commands, try to initialize the application
$app = newApplication($config);
$app->handleCommand($argv);
} catch (Exception$e) {
error("Error: " . $e->getMessage());
// If database connection failed, show available offline commands
if (strpos($e->getMessage(), 'Database connection failed') !== false) {
echo"\n" . color("Available offline commands:", 'yellow') . "\n";
echo" php crypto-cli.php generate-keys\n";
echo" php crypto-cli.php encrypt-test\n";
echo" php crypto-cli.php help\n";
}
exit(1);
}