Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Nettle
nettle
Commits
f3f395ea
Commit
f3f395ea
authored
Apr 14, 2012
by
Niels Möller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New base16 example programs, based on code contributed by Jeronimo Pellegrini.
parent
435e8626
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
237 additions
and
4 deletions
+237
-4
ChangeLog
ChangeLog
+6
-2
examples/Makefile.in
examples/Makefile.in
+12
-2
examples/base16dec.c
examples/base16dec.c
+112
-0
examples/base16enc.c
examples/base16enc.c
+107
-0
No files found.
ChangeLog
View file @
f3f395ea
...
...
@@ -2,11 +2,15 @@
* examples/io.c (write_file): Use write_string.
* examples/Makefile.in (base64enc): New target. Also added missing
io.o dependency to several other targets.
* examples/Makefile.in (base64enc): New targets. Also
added missing io.o dependency to several other targets.
(base64dec, base16enc, base16dec): Likewise.
* examples/base64enc.c: New file, based on example code
contributed by Jeronimo Pellegrini.
* examples/base64dec.c: Likewise.
* examples/base16enc.c: Likewise.
* examples/base16dec.c: Likewise.
* examples/rsa-encrypt.c (process_file): Reorganized fread loop.
(usage): New function.
...
...
examples/Makefile.in
View file @
f3f395ea
...
...
@@ -16,13 +16,15 @@ BENCH_LIBS = @BENCH_LIBS@
RSA_TARGETS
=
rsa-keygen
$(EXEEXT)
rsa-sign
$(EXEEXT)
\
rsa-verify
$(EXEEXT)
rsa-encrypt
$(EXEEXT)
rsa-decrypt
$(EXEEXT)
ENC_TARGETS
=
base16enc
$(EXEEXT)
base16dec
$(EXEEXT)
\
base64enc
$(EXEEXT)
base64dec
$(EXEEXT)
TARGETS
=
nettle-benchmark
$(EXEEXT)
eratosthenes
$(EXEEXT)
\
base64enc
$(EXEEXT)
base64dec
$(EXEEXT
)
@IF_HOGWEED@
$(RSA_TARGETS)
next-prime
$(EXEEXT)
random-prime
$(EXEEXT)
$(ENC_TARGETS
)
@IF_HOGWEED@
$(RSA_TARGETS)
next-prime
$(EXEEXT)
random-prime
$(EXEEXT)
SOURCES
=
nettle-benchmark.c eratosthenes.c next-prime.c random-prime.c
\
nettle-openssl.c
\
io.c read_rsa_key.c
\
rsa-encrypt.c rsa-decrypt.c rsa-keygen.c rsa-sign.c rsa-verify.c
\
base64enc.c base64dec.c
base16enc.c
base64enc.c base64dec.c
GETOPT_OBJS
=
../getopt.
$(OBJEXT)
../getopt1.
$(OBJEXT)
...
...
@@ -70,6 +72,14 @@ rsa-decrypt$(EXEEXT): rsa-decrypt.$(OBJEXT) io.$(OBJEXT) read_rsa_key.$(OBJEXT)
$(LINK)
rsa-decrypt.
$(OBJEXT)
io.
$(OBJEXT)
read_rsa_key.
$(OBJEXT)
\
-lhogweed
-lnettle
$(LIBS)
-o
rsa-decrypt
$(EXEEXT)
base16enc$(EXEEXT)
:
base16enc.$(OBJEXT) io.$(OBJEXT)
$(LINK)
base16enc.
$(OBJEXT)
io.
$(OBJEXT)
\
-lnettle
$(LIBS)
-o
base16enc
$(EXEEXT)
base16dec$(EXEEXT)
:
base16dec.$(OBJEXT) io.$(OBJEXT)
$(LINK)
base16dec.
$(OBJEXT)
io.
$(OBJEXT)
\
-lnettle
$(LIBS)
-o
base16dec
$(EXEEXT)
base64enc$(EXEEXT)
:
base64enc.$(OBJEXT) io.$(OBJEXT)
$(LINK)
base64enc.
$(OBJEXT)
io.
$(OBJEXT)
\
-lnettle
$(LIBS)
-o
base64enc
$(EXEEXT)
...
...
examples/base16dec.c
0 → 100644
View file @
f3f395ea
/* base16dec -- an decoder for base16
*
* Copyright (C) 2006, 2012 Jeronimo Pellegrini, Niels Möller
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the nettle library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <nettle/base16.h>
#include "io.h"
#define CHUNK_SIZE 16392
/* The maximum number of bytes generated for each chunk: */
#define DECODED_SIZE BASE16_DECODE_LENGTH(CHUNK_SIZE)
/*
* Reads base-16 encoded from stdin, writes decoded to stdout.
*/
int
main
(
int
argc
UNUSED
,
char
**
argv
UNUSED
)
{
/* "buffer" will hold the bytes from disk: */
uint8_t
*
buffer
=
xalloc
(
CHUNK_SIZE
);
/* "result" will hold bytes before output: */
uint8_t
*
result
=
xalloc
(
DECODED_SIZE
);
/* We need a Base16 context for decoding: */
struct
base16_decode_ctx
b16_ctx
;
/* Init the context: */
base16_decode_init
(
&
b16_ctx
);
#ifdef WIN32
_setmode
(
1
,
O_BINARY
);
#endif
for
(;;)
{
int
nbytes
;
/* Number of bytes read frmo disk at each iteration */
unsigned
decoded_bytes
;
/* Bytes actually generated at each iteration */
nbytes
=
fread
(
buffer
,
1
,
CHUNK_SIZE
,
stdin
);
if
(
nbytes
<
CHUNK_SIZE
&&
ferror
(
stdin
))
{
werror
(
"Error reading file: %s
\n
"
,
strerror
(
errno
));
return
EXIT_FAILURE
;
}
decoded_bytes
=
BASE16_DECODE_LENGTH
(
nbytes
);
/* Decodes one chunk: */
if
(
!
base16_decode_update
(
&
b16_ctx
,
&
decoded_bytes
,
result
,
nbytes
,
buffer
))
{
werror
(
"Error decoding input (not base16?)
\n
"
);
return
EXIT_FAILURE
;
}
if
(
!
write_string
(
stdout
,
decoded_bytes
,
result
))
{
werror
(
"Error writing file: %s
\n
"
,
strerror
(
errno
));
return
EXIT_FAILURE
;
}
if
(
nbytes
<
CHUNK_SIZE
)
{
/* Check if decoding finalized OK: */
if
(
!
base16_decode_final
(
&
b16_ctx
))
{
werror
(
"Decoding did not finish properly.
\n
"
);
return
EXIT_FAILURE
;
}
break
;
}
}
if
(
fflush
(
stdout
)
!=
0
)
{
werror
(
"Error writing file: %s
\n
"
,
strerror
(
errno
));
return
EXIT_FAILURE
;
}
free
(
buffer
);
free
(
result
);
return
EXIT_SUCCESS
;
}
examples/base16enc.c
0 → 100644
View file @
f3f395ea
/* base16enc -- an encoder for base16
*
* Copyright (C) 2006, 2012 Jeronimo Pellegrini, Niels Möller
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the nettle library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <nettle/base16.h>
#include "io.h"
/* The number of bytes read in each iteration, we do one line at a time: */
#define CHUNK_SIZE 36
/* The *maximum* size of an encoded chunk: */
#define ENCODED_SIZE BASE16_ENCODE_LENGTH(CHUNK_SIZE)
/*
* Reads bytes from standard input and writes base64-encoded
* on standard output.
*/
int
main
(
int
argc
UNUSED
,
char
**
argv
UNUSED
)
{
/* "buffer" will hold the bytes from disk: */
uint8_t
*
buffer
=
(
uint8_t
*
)
malloc
(
CHUNK_SIZE
*
sizeof
(
uint8_t
));
if
(
buffer
==
NULL
)
{
fprintf
(
stderr
,
"Cannot allocate read buffer.
\n
"
);
return
EXIT_FAILURE
;
}
/* "result" will hold bytes before output: */
uint8_t
*
result
=
(
uint8_t
*
)
malloc
(
ENCODED_SIZE
*
sizeof
(
uint8_t
));
if
(
result
==
NULL
)
{
fprintf
(
stderr
,
"Cannot allocate write buffer.
\n
"
);
return
EXIT_FAILURE
;
}
#ifdef WIN32
_setmode
(
0
,
O_BINARY
);
#endif
/* There is no context to initialize. */
for
(;;)
{
/* "buffer" will hold the bytes from disk: */
uint8_t
buffer
[
CHUNK_SIZE
];
/* "result" will hold bytes before output: */
uint8_t
result
[
ENCODED_SIZE
+
1
];
unsigned
nbytes
;
/* Number of bytes read from stdin */
int
encoded_bytes
;
/* Total number of bytes encoded per iteration */
nbytes
=
fread
(
buffer
,
1
,
CHUNK_SIZE
,
stdin
);
/* We overwrite result with more data */
base16_encode_update
(
result
,
nbytes
,
buffer
);
encoded_bytes
=
BASE16_ENCODE_LENGTH
(
nbytes
);
result
[
encoded_bytes
++
]
=
'\n'
;
if
(
nbytes
<
CHUNK_SIZE
)
{
if
(
ferror
(
stdin
))
{
werror
(
"Error reading file: %s
\n
"
,
strerror
(
errno
));
return
EXIT_FAILURE
;
}
if
(
!
write_string
(
stdout
,
encoded_bytes
,
result
)
||
fflush
(
stdout
)
!=
0
)
{
werror
(
"Error writing file: %s
\n
"
,
strerror
(
errno
));
return
EXIT_FAILURE
;
}
return
EXIT_SUCCESS
;
}
/* The result vector is printed */
if
(
!
write_string
(
stdout
,
encoded_bytes
,
result
))
{
werror
(
"Error writing file: %s
\n
"
,
strerror
(
errno
));
return
EXIT_FAILURE
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment