Skip to content

Commit

Permalink
keygen tools (#305)
Browse files Browse the repository at this point in the history
* add keygen tools

* move keygen test to integration tests
  • Loading branch information
Lagovas committed Feb 1, 2018
1 parent e721d21 commit d89de67
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jobs:
- run: sudo /sbin/ldconfig
- run: chmod u+x ./tests/_integration/integration_total.sh
- run: ./tests/_integration/integration_total.sh
- run: bash tests/tools/check_keygen.sh


# using this php5 image until we ensure tests are working for php7
Expand Down
28 changes: 28 additions & 0 deletions tests/tools/check_keygen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

. tests/_integration/utils.sh

test_keygen () {
rm --force key key.pub test_key test_key.pub
bash -c "$1 $2"
check_result_zero
stat key > /dev/null
check_result_zero
stat key.pub > /dev/null
check_result_zero

bash -c "$1 $2 test_key test_key.pub"
check_result_zero
stat test_key > /dev/null
check_result_zero
stat test_key.pub > /dev/null
check_result_zero
}

test_keygen python tools/python/keygen.py
test_keygen node tools/js/keygen.js
test_keygen "php -f" tools/php/keygen.php
test_keygen ruby tools/ruby/keygen.rb
test_keygen "go run" tools/go/keygen.go

exit ${status}
39 changes: 39 additions & 0 deletions tools/go/keygen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"github.com/cossacklabs/themis/gothemis/keys"
"io/ioutil"
"os"
)

func main() {
args_count := len(os.Args)

if args_count != 1 && args_count != 3 {
fmt.Printf("Usage: go %s <private_key_path> <public_key_path>\n", os.Args[0])
os.Exit(1)
}

var private_key_path, public_key_path string
if args_count == 1 {
private_key_path = "key"
public_key_path = "key.pub"
} else if args_count == 3 {
private_key_path = os.Args[1]
public_key_path = os.Args[2]
}

keypair, err := keys.New(keys.KEYTYPE_EC)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(private_key_path, keypair.Private.Value, 0400)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(public_key_path, keypair.Public.Value, 0666)
if err != nil {
panic(err)
}
}
20 changes: 20 additions & 0 deletions tools/js/keygen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const jsthemis = require('jsthemis');
const fs = require('fs');

if (process.argv.length !== 2 && process.argv.length !== 4) {
console.log('Usage: node keygen.js <private_key_path> <public_key_path>');
process.exit(1);
}

if (process.argv.length === 2) {
var private_key_path = 'key';
var public_key_path = 'key.pub';
} else if (process.argv.length === 4){
var private_key_path = process.argv[2];
var public_key_path = process.argv[3];
}

keypair = new jsthemis.KeyPair();

fs.writeFileSync(private_key_path, keypair.private(), {'mode': 0o400});
fs.writeFileSync(public_key_path, keypair.public());
40 changes: 40 additions & 0 deletions tools/php/keygen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
#
# Copyright (c) 2015 Cossack Labs Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


if (!extension_loaded('phpthemis')) die("no phpthemis extention found!\n");

if (count($argv) != 1 && count($argv) != 3) {
die("Usage: php -f keygen.php <private_key_path> <public_key_path>\n");
}
$private_key_path = null;
$public_key_path = null;
if (count($argv) == 1){
$private_key_path = "key";
$public_key_path = "key.pub";
} else if (count($argv) == 3){
$private_key_path = $argv[1];
$public_key_path = $argv[2];
}

$keypair = phpthemis_gen_ec_key_pair();

file_put_contents($private_key_path, $keypair['private_key']);
chmod($private_key_path, 0400);
file_put_contents($public_key_path, $keypair['public_key']);

?>
44 changes: 44 additions & 0 deletions tools/python/keygen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright (c) 2015 Cossack Labs Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os
from pythemis.skeygen import KEY_PAIR_TYPE, GenerateKeyPair

args_count = len(sys.argv)
if args_count != 3 and args_count != 1:
print("Usage: python {} <private_key_path> <public_key_path>".format(
os.path.basename(__file__)))
exit(1)

keypair = GenerateKeyPair(KEY_PAIR_TYPE.EC)
private_key = keypair.export_private_key()
public_key = keypair.export_public_key()
if args_count == 1:
private_key_path = 'key'
public_key_path = 'key.pub'
elif args_count == 3:
private_key_path = sys.argv[1]
public_key_path = sys.argv[2]


with open(private_key_path, "wb") as private_key_file:
private_key_file.write(private_key)

os.chmod(private_key_path, 0o400)

with open(public_key_path, "wb") as public_key_file:
public_key_file.write(public_key)
47 changes: 47 additions & 0 deletions tools/ruby/keygen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Copyright (c) 2015 Cossack Labs Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#!/usr/bin/env ruby

require 'fileutils'
require 'rubygems'
require 'rubythemis'

if ARGV.length != 0 && ARGV.length != 2
puts "Usage: ruby keygen.rb <private_key_path> <public_key_path>"
exit 1
end

generator = Themis::SKeyPairGen.new

if ARGV.length == 0
private_key_path = 'key'
public_key_path = 'key.pub'
elsif ARGV.length == 2
private_key_path = ARGV[0]
public_key_path = ARGV[1]
end
keypair = generator.ec

File.open(private_key_path, 'w') { |file|
file.write(keypair[0])
}

FileUtils.chmod 0400, private_key_path

File.open(public_key_path, 'w') { |file|
file.write(keypair[1])
}

0 comments on commit d89de67

Please sign in to comment.