1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

move crypto functions into utils

This commit is contained in:
Steffen Vogel 2019-05-28 10:55:24 +02:00
parent c807dbadf4
commit 5dbaa39b88
7 changed files with 85 additions and 90 deletions

View file

@ -26,7 +26,7 @@
#include <curl/curl.h>
#include <villas/crypt.h>
#include <villas/utils.hpp>
struct advio {
CURL *curl;

View file

@ -1,33 +0,0 @@
/** Crypto helpers.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
#include <stdio.h>
#include <openssl/sha.h>
/** Calculate SHA1 hash of complete file \p f and place it into \p sha1.
*
* @param sha1[out] Must be SHA_DIGEST_LENGTH (20) in size.
* @retval 0 Everything was okay.
*/
int sha1sum(FILE *f, unsigned char *sha1);

View file

@ -25,7 +25,6 @@ add_library(villas-common SHARED
buffer.cpp
json_buffer.cpp
compat.cpp
crypt.cpp
hist.cpp
dsp/pid.cpp
kernel/kernel.cpp

View file

@ -35,7 +35,6 @@
#include <villas/utils.hpp>
#include <villas/config.h>
#include <villas/advio.h>
#include <villas/crypt.h>
#define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */

View file

@ -1,49 +0,0 @@
/** Crypto helpers.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <villas/crypt.h>
int sha1sum(FILE *f, unsigned char *sha1)
{
SHA_CTX c;
char buf[512];
ssize_t bytes;
long seek;
seek = ftell(f);
fseek(f, 0, SEEK_SET);
SHA1_Init(&c);
bytes = fread(buf, 1, 512, f);
while (bytes > 0) {
SHA1_Update(&c, buf, bytes);
bytes = fread(buf, 1, 512, f);
}
SHA1_Final(sha1, &c);
fseek(f, seek, SEEK_SET);
return 0;
}

View file

@ -36,6 +36,10 @@
#include <ctype.h>
#include <fcntl.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <villas/config.h>
#include <villas/utils.hpp>
#include <villas/colors.hpp>
@ -47,15 +51,14 @@ static pthread_t main_thread;
namespace villas {
namespace utils {
std::vector<std::string>
tokenize(std::string s, std::string delimiter)
std::vector<std::string> tokenize(std::string s, std::string delimiter)
{
std::vector<std::string> tokens;
size_t lastPos = 0;
size_t curentPos;
while((curentPos = s.find(delimiter, lastPos)) != std::string::npos) {
while ((curentPos = s.find(delimiter, lastPos)) != std::string::npos) {
const size_t tokenLength = curentPos - lastPos;
tokens.push_back(s.substr(lastPos, tokenLength));
@ -64,7 +67,7 @@ tokenize(std::string s, std::string delimiter)
}
/* Check if there's a last token behind the last delimiter. */
if(lastPos != s.length()) {
if (lastPos != s.length()) {
const size_t lastTokenLength = s.length() - lastPos;
tokens.push_back(s.substr(lastPos, lastTokenLength));
}
@ -328,5 +331,82 @@ size_t strlenp(const char *str)
return sz;
}
int sha1sum(FILE *f, unsigned char *sha1)
{
SHA_CTX c;
char buf[512];
ssize_t bytes;
long seek;
seek = ftell(f);
fseek(f, 0, SEEK_SET);
SHA1_Init(&c);
bytes = fread(buf, 1, 512, f);
while (bytes > 0) {
SHA1_Update(&c, buf, bytes);
bytes = fread(buf, 1, 512, f);
}
SHA1_Final(sha1, &c);
fseek(f, seek, SEEK_SET);
return 0;
}
namespace base64 {
std::string encode(const std::string &str)
{
return encode((unsigned char *) str.data(), str.size());
}
std::string decode(const std::string &str)
{
return decode((unsigned char *) str.data(), str.size());
}
std::string encode(const unsigned char *input, size_t len)
{
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input, len);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
std::string str(bptr->data, bptr->length);
BIO_free_all(b64);
return str;
}
std::string decode(unsigned char *input, size_t len)
{
BIO *b64, *bmem;
std::string str(len, 0);
char *buffer = (char *) malloc(len);
memset(buffer, 0, len);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(input, len);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, str.data(), str.capacity());
BIO_free_all(bmem);
return buffer;
}
} /* namespace base64 */
} /* namespace utils */
} /* namespace villas */

View file

@ -22,7 +22,6 @@
#include <criterion/criterion.h>
#include <villas/crypt.h>
#include <villas/colors.hpp>
#include <villas/utils.hpp>
#include <villas/version.hpp>