这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions test/c/05-sign-keypair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var nacl = require('../../' + (process.env.NACL_SRC || 'nacl.min.js'));
var crypto = require('crypto');
var spawn = require('child_process').spawn;
var execFile = require('child_process').execFile;
var path = require('path');
var test = require('tape');

function csign(sk, msg, callback) {
var hexsk = (new Buffer(sk)).toString('hex');
var p = spawn(path.resolve(__dirname, 'csign'), [hexsk]);
var result = [];
p.stdout.on('data', function(data) {
result.push(data);
});
p.on('close', function(code) {
callback(Buffer.concat(result).toString('base64'));
});
p.on('error', function(err) {
throw err;
});
p.stdin.write(msg);
p.stdin.end();
}

function csignkeypair(callback) {
execFile(path.resolve(__dirname, 'csign-keypair'), [], function(err, stdout) {
if (err) throw err;
callback(stdout.toString('utf8'));
});
}

test('nacl.sign (C) with keypair from C', function(t) {
function check(num) {
csignkeypair(function(hexSecretKey) {
var secretKey = new Uint8Array(nacl.sign.secretKeyLength);
var b = new Buffer(hexSecretKey, 'hex');
for (var i = 0; i < b.length; i++) secretKey[i] = b[i];
var msg = nacl.randomBytes(num);
var signedMsg = nacl.util.encodeBase64(nacl.sign(msg, secretKey));
csign(secretKey, new Buffer(msg), function(signedFromC) {
t.equal(signedMsg, signedFromC, 'signed messages should be equal');
if (num >= 100) {
t.end();
return;
}
check(num+1);
});
});
}

check(0);
});
21 changes: 12 additions & 9 deletions test/c/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CFLAGS=-O3

all: csecretbox cscalarmult cbox chash csign
all: csecretbox cscalarmult cbox chash csign csign-keypair

csecretbox: csecretbox.o tweetnacl.o

Expand All @@ -12,24 +12,27 @@ chash: chash.o tweetnacl.o

csign: csign.o tweetnacl.o

test: test_secretbox
csign-keypair: csign-keypair.o tweetnacl.o

#test_scalarmult test_box test_hash test_sign clean
test: test_secretbox test_scalarmult test_box test_hash test_sign test_signkeypair clean

test_secretbox: csecretbox
tap --timeout 1800 ./secretbox.js
node ./00-secretbox.js

test_scalarmult: cscalarmult
node ./scalarmult.js
node ./01-scalarmult.js

test_box: cbox
node ./box.js
node ./02-box.js

test_hash: chash
node ./hash.js
node ./03-hash.js

test_sign: csign
node ./sign.js
node ./04-sign.js

test_signkeypair: csign csign-keypair
node ./05-sign-keypair.js

clean:
rm *.o csecretbox cscalarmult cbox chash csign
rm *.o csecretbox cscalarmult cbox chash csign csign-keypair
2 changes: 1 addition & 1 deletion test/c/chash.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This program sings a message using tweenacl.c.
* This program sings a message using tweetnacl.c.
* Written by @dchest. Public domain.
*/
#include <err.h>
Expand Down
53 changes: 53 additions & 0 deletions test/c/csign-keypair.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This program generates a sign secret key using tweetnacl.c.
* Written by @dchest. Public domain.
*/
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "tweetnacl.h"

void randombytes(unsigned char *x,unsigned long long xlen) {
static int fd = -1;
int i;

if (fd == -1) {
for (;;) {
fd = open("/dev/urandom",O_RDONLY);
if (fd != -1) break;
sleep(1);
}
}

while (xlen > 0) {
if (xlen < 1048576) i = xlen; else i = 1048576;

i = read(fd,x,i);
if (i < 1) {
sleep(1);
continue;
}

x += i;
xlen -= i;
}
}

int
main(int argc, const char **argv)
{
unsigned char pk[crypto_sign_PUBLICKEYBYTES],
sk[crypto_sign_SECRETKEYBYTES];
int i;

crypto_sign_keypair(pk, sk);
for (i = 0; i < sizeof(sk); i++)
printf("%02x", sk[i]);
return 0;
}
2 changes: 1 addition & 1 deletion test/c/csign.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This program sings a message using tweenacl.c.
* This program signs a message using tweetnacl.c.
* Written by @dchest. Public domain.
*/
#include <err.h>
Expand Down