mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-09 15:33:58 +08:00
Update some demos
This commit is contained in:
37
demos/otp/otp-setup.c
Normal file
37
demos/otp/otp-setup.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <libgen.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/is_gmssl.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
BIO *bio = NULL;
|
||||
unsigned char key[32];
|
||||
|
||||
if (!RAND_bytes(key, sizeof(key))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(bio = BIO_new_file(".otp_secret", "w"))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (BIO_write(bio, key, sizeof(key)) != sizeof(key)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
BIO_free(bio);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("generate OTP seed in '.otp_secret'\n");
|
||||
|
||||
BIO_free(bio);
|
||||
OPENSSL_cleanse(key, sizeof(key));
|
||||
|
||||
return 0;
|
||||
}
|
||||
43
demos/otp/otp.c
Normal file
43
demos/otp/otp.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <libgen.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/otp.h>
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/is_gmssl.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *prog = basename(argv[0]);
|
||||
BIO *bio = NULL;
|
||||
OTP_PARAMS params;
|
||||
unsigned char key[32] = {0};
|
||||
unsigned char event[] = "this is a fixed value";
|
||||
unsigned int otp;
|
||||
|
||||
params.type = NID_sm3;
|
||||
params.te = 1;
|
||||
params.option = NULL;
|
||||
params.option_size = 0;
|
||||
params.otp_digits = 6;
|
||||
|
||||
if (!(bio = BIO_new_file(".otp_secret", "r"))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return -1;
|
||||
}
|
||||
if (BIO_read(bio, key, sizeof(key)) != sizeof(key)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
BIO_free(bio);
|
||||
return -1;
|
||||
}
|
||||
BIO_free(bio);
|
||||
|
||||
if (!OTP_generate(¶ms, event, sizeof(event), &otp, key, sizeof(key))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%06u\n", otp);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user