Skip to content

Commit ebf7d0b

Browse files
authored
Merge pull request #16 from i2p/generateKeys
fix public key out put in generateKeys
2 parents 8623304 + b410da5 commit ebf7d0b

File tree

5 files changed

+73
-15
lines changed

5 files changed

+73
-15
lines changed

.gitignore

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
*.o
22
libsam3-tests
3-
*.a
3+
*.a
4+
examples/libsam3
5+
examples/sam3/dgrams
6+
examples/sam3/dgramc
7+
examples/sam3/keys
8+
examples/sam3/keysp
9+
examples/sam3namelookup
10+
examples/sam3/streamss
11+
examples/sam3/streamcs
12+
examples/sam3/log
13+
examples/sam3/err
14+

examples/sam3/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ ssclient:
2828
ssserver:
2929
${CC} ${CFLAGS} streamss.c -o streamss ../libsam3/libsam3.o
3030

31+
keysp:
32+
${CXX} ${CFLAGS} keys.cc -o keysp ../libsam3/libsam3.o
33+
34+
keys:
35+
${CC} ${CFLAGS} keys.c -o keys ../libsam3/libsam3.o
3136

3237
clean:
33-
rm -f samtest lookup dgramc dgrams streamc streams streams.key test-lookup
38+
rm -f samtest lookup dgramc dgrams streamc streams streams.key test-lookup keys keysp
3439

3540
debug:
3641
sed -i 's|// libsam3_debug = 1;|libsam3_debug = 1;|g' *.c

examples/sam3/keys.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//#include <string>
2+
//#include <iostream>
3+
#include "../libsam3/libsam3.h"
4+
#include <stdio.h>
5+
6+
int main() {
7+
// The session is only usef for transporting the data
8+
Sam3Session ss;
9+
10+
if (0 > sam3GenerateKeys(&ss, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, 4)) {
11+
printf("got error");
12+
return -1;
13+
}
14+
printf("\tpubkey: %s \n \tprivkey: %s", ss.pubkey, ss.privkey);
15+
/*auto pub = std::string(ss.pubkey);
16+
auto priv = std::string(ss.privkey);
17+
18+
std::cout << "pub " << pub << std::endl << "priv " << priv << std::endl;*/
19+
return 0;
20+
}

examples/sam3/keys.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <string>
2+
#include <iostream>
3+
#include <stdio.h>
4+
#include "../libsam3/libsam3.h"
5+
6+
int main() {
7+
// The session is only usef for transporting the data
8+
Sam3Session ss;
9+
10+
if (0 > sam3GenerateKeys(&ss, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, Sam3SigType::EdDSA_SHA512_Ed25519)) {
11+
printf("got error");
12+
return -1;
13+
}
14+
auto pub = std::string(ss.pubkey);
15+
auto priv = std::string(ss.privkey);
16+
17+
std::cout << "pub " << pub << std::endl << "priv " << priv << std::endl;
18+
return 0;
19+
}

src/libsam3/libsam3.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -716,20 +716,23 @@ int sam3GenerateKeys(Sam3Session *ses, const char *hostname, int port,
716716
return -1;
717717
}
718718
//
719-
if (sam3tcpPrintf(fd, "DEST GENERATE %s\n", sigtypes[sigType]) >= 0) {
720-
if ((rep = sam3ReadReply(fd)) != NULL &&
721-
sam3IsGoodReply(rep, "DEST", "REPLY", NULL, NULL)) {
722-
const char *pub = sam3FindField(rep, "PUB"),
723-
*priv = sam3FindField(rep, "PRIV");
724-
//
725-
if (pub != NULL && sam3CheckValidKeyLength(pub) && priv != NULL &&
726-
strlen(priv) >= SAM3_PRIVKEY_MIN_SIZE) {
727-
strcpy(ses->pubkey, pub);
728-
strcpy(ses->privkey, priv);
729-
res = 0;
730-
}
731-
}
719+
if (sam3tcpPrintf(fd, "DEST GENERATE\n") < 0) {
720+
strcpyerr(ses, "DEST_ERROR");
721+
}
722+
723+
rep = sam3ReadReply(fd);
724+
// sam3DumpFieldList(rep);
725+
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PUB", NULL)) {
726+
strcpyerr(ses, "PUBKEY_ERROR");
727+
}
728+
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PRIV", NULL)) {
729+
strcpyerr(ses, "PRIVKEY_ERROR");
732730
}
731+
const char *pub = sam3FindField(rep, "PUB");
732+
strcpy(ses->pubkey, pub);
733+
const char *priv = sam3FindField(rep, "PRIV");
734+
strcpy(ses->privkey, priv);
735+
res = 0;
733736
//
734737
sam3FreeFieldList(rep);
735738
sam3tcpDisconnect(fd);

0 commit comments

Comments
 (0)