Loading...
Searching...
No Matches
key_generator_tools.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24
26
27using namespace resdb;
28
29void WriteKey(const KeyInfo& key, const std::string& file_name) {
30 std::string str;
31 assert(key.SerializeToString(&str));
32
33 printf("save key to path %s\n", file_name.c_str());
34 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT, 0666);
35 if (fd < 0) {
36 printf("open file fail %s\n", strerror(errno));
37 }
38 assert(fd >= 0);
39 write(fd, str.c_str(), str.size());
40 close(fd);
41}
42
43int main(int argc, char** argv) {
44 std::string type = "ED25519";
45 std::string path = "./";
46 if (argc < 2) {
47 printf("<path> [type](RSA|AES|ED25519(default))\n");
48 exit(0);
49 } else {
50 path = argv[1];
51 if (argc > 2) {
52 type = argv[2];
53 }
54 }
55 SignatureInfo::HashType hash_type;
56 if (type == "RSA") {
57 hash_type = SignatureInfo::RSA;
58 } else if (type == "AES") {
59 hash_type = SignatureInfo::CMAC_AES;
60 } else if (type == "ED25519") {
61 hash_type = SignatureInfo::ED25519;
62 } else {
63 printf("type invalid\n");
64 exit(0);
65 }
66
67 SecretKey key = KeyGenerator::GeneratorKeys(hash_type);
68
69 std::string str;
70 assert(key.SerializeToString(&str));
71
72 // Save private key and public key to files.
73 KeyInfo pub_info, pri_info;
74 pub_info.set_key(key.public_key());
75 pub_info.set_hash_type(key.hash_type());
76
77 pri_info.set_key(key.private_key());
78 pri_info.set_hash_type(key.hash_type());
79
80 WriteKey(pri_info, path + ".key.pri");
81 WriteKey(pub_info, path + ".key.pub");
82}
static SecretKey GeneratorKeys(SignatureInfo::HashType type)
int main(int argc, char **argv)
void WriteKey(const KeyInfo &key, const std::string &file_name)