[DEV] add basic algo (base of libcripto)
This commit is contained in:
parent
afd16ca396
commit
4df549c566
4
.gitignore
vendored
4
.gitignore
vendored
@ -27,3 +27,7 @@
|
|||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
|
#python temporary file
|
||||||
|
__pycache__
|
||||||
|
*.pyc
|
||||||
|
111
algue/base64.cpp
Normal file
111
algue/base64.cpp
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#include <etk/types.h>
|
||||||
|
#include <algue/base64.h>
|
||||||
|
|
||||||
|
static const std::string base64Elements = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
|
const std::vector<uint8_t>& getReverseTable() {
|
||||||
|
static std::vector<uint8_t> table;
|
||||||
|
if (table.size() == 0) {
|
||||||
|
table.resize(256,0);
|
||||||
|
for (size_t iii=0; iii<base64Elements.size(); ++iii) {
|
||||||
|
table[size_t(base64Elements[iii])] = iii;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static bool isBase64(char _ccc) {
|
||||||
|
return isalnum(_ccc)
|
||||||
|
|| _ccc == '+'
|
||||||
|
|| _ccc == '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string algue::base64::encode(const uint8_t* _data, int32_t _len) {
|
||||||
|
std::string out;
|
||||||
|
int32_t iii = 0;
|
||||||
|
int32_t jjj = 0;
|
||||||
|
uint8_t bit3[3];
|
||||||
|
uint8_t bit4[4];
|
||||||
|
// Pack 3 bytes in 4 bytes
|
||||||
|
while (_len--) {
|
||||||
|
bit3[iii++] = *(_data++);
|
||||||
|
if (iii == 3) {
|
||||||
|
iii = 0;
|
||||||
|
bit4[0] = (bit3[0] & 0xfc) >> 2;
|
||||||
|
bit4[1] = ((bit3[0] & 0x03) << 4) + ((bit3[1] & 0xf0) >> 4);
|
||||||
|
bit4[2] = ((bit3[1] & 0x0f) << 2) + ((bit3[2] & 0xc0) >> 6);
|
||||||
|
bit4[3] = bit3[2] & 0x3f;
|
||||||
|
for(int32_t jjj=0; jjj<4 ; ++jjj) {
|
||||||
|
out += base64Elements[bit4[jjj]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Residual packing ...
|
||||||
|
if (iii != 0) {
|
||||||
|
for(int32_t jjj=iii; jjj<3; ++jjj) {
|
||||||
|
bit3[jjj] = 0;
|
||||||
|
}
|
||||||
|
bit4[0] = (bit3[0] & 0xfc) >> 2;
|
||||||
|
bit4[1] = ((bit3[0] & 0x03) << 4) + ((bit3[1] & 0xf0) >> 4);
|
||||||
|
bit4[2] = ((bit3[1] & 0x0f) << 2) + ((bit3[2] & 0xc0) >> 6);
|
||||||
|
bit4[3] = bit3[2] & 0x3f;
|
||||||
|
for (int32_t jjj=0; jjj<iii+1; ++jjj) {
|
||||||
|
out += base64Elements[bit4[jjj]];
|
||||||
|
}
|
||||||
|
while (iii++ < 3) {
|
||||||
|
out += '=';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> algue::base64::decode(const std::string& _data) {
|
||||||
|
int32_t len = _data.size();
|
||||||
|
int32_t iii = 0;
|
||||||
|
int32_t id = 0;
|
||||||
|
uint8_t bit4[4];
|
||||||
|
uint8_t bit3[3];
|
||||||
|
std::vector<uint8_t> out;
|
||||||
|
const std::vector<uint8_t>& base64ElementsReverse = getReverseTable();
|
||||||
|
while ( len--
|
||||||
|
&& _data[id] != '='
|
||||||
|
&& isBase64(_data[id]) == true) {
|
||||||
|
bit4[iii++] = _data[id];
|
||||||
|
id++;
|
||||||
|
if (iii == 4) {
|
||||||
|
iii = 0;
|
||||||
|
for (int32_t jjj=0; jjj<4; ++jjj) {
|
||||||
|
bit4[jjj] = base64ElementsReverse[bit4[jjj]];
|
||||||
|
}
|
||||||
|
bit3[0] = (bit4[0] << 2) + ((bit4[1] & 0x30) >> 4);
|
||||||
|
bit3[1] = ((bit4[1] & 0xf) << 4) + ((bit4[2] & 0x3c) >> 2);
|
||||||
|
bit3[2] = ((bit4[2] & 0x3) << 6) + bit4[3];
|
||||||
|
for (int32_t jjj=0; jjj<3; ++jjj) {
|
||||||
|
out.push_back(bit3[jjj]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (iii != 0) {
|
||||||
|
for (int32_t jjj=iii; jjj<4; ++jjj) {
|
||||||
|
bit4[jjj] = 0;
|
||||||
|
}
|
||||||
|
for (int32_t jjj = 0; jjj <4; ++jjj) {
|
||||||
|
bit4[jjj] = base64ElementsReverse[bit4[jjj]];
|
||||||
|
}
|
||||||
|
bit3[0] = (bit4[0] << 2) + ((bit4[1] & 0x30) >> 4);
|
||||||
|
bit3[1] = ((bit4[1] & 0xf) << 4) + ((bit4[2] & 0x3c) >> 2);
|
||||||
|
bit3[2] = ((bit4[2] & 0x3) << 6) + bit4[3];
|
||||||
|
for (int32_t jjj=0; jjj<iii-1; ++jjj) {
|
||||||
|
out.push_back(bit3[jjj]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
21
algue/base64.h
Normal file
21
algue/base64.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <etk/types.h>
|
||||||
|
|
||||||
|
namespace algue {
|
||||||
|
namespace base64 {
|
||||||
|
std::string encode(const uint8_t* _data, int32_t _len);
|
||||||
|
inline std::string encode(const std::vector<uint8_t>& _data) {
|
||||||
|
return algue::base64::encode(&_data[0], _data.size());
|
||||||
|
}
|
||||||
|
inline std::string encode(const std::string& _data) {
|
||||||
|
return algue::base64::encode(reinterpret_cast<const uint8_t*>(&_data[0]), _data.size());
|
||||||
|
}
|
||||||
|
std::vector<uint8_t> decode(const std::string& _data);
|
||||||
|
}
|
||||||
|
}
|
13
algue/debug.cpp
Normal file
13
algue/debug.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <algue/debug.h>
|
||||||
|
|
||||||
|
int32_t algue::getLogId() {
|
||||||
|
static int32_t g_val = elog::registerInstance("algue");
|
||||||
|
return g_val;
|
||||||
|
}
|
||||||
|
|
37
algue/debug.h
Normal file
37
algue/debug.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <elog/log.h>
|
||||||
|
|
||||||
|
namespace algue {
|
||||||
|
int32_t getLogId();
|
||||||
|
};
|
||||||
|
#define ALGUE_BASE(info,data) ELOG_BASE(algue::getLogId(),info,data)
|
||||||
|
|
||||||
|
#define ALGUE_CRITICAL(data) ALGUE_BASE(1, data)
|
||||||
|
#define ALGUE_ERROR(data) ALGUE_BASE(2, data)
|
||||||
|
#define ALGUE_WARNING(data) ALGUE_BASE(3, data)
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define ALGUE_INFO(data) ALGUE_BASE(4, data)
|
||||||
|
#define ALGUE_DEBUG(data) ALGUE_BASE(5, data)
|
||||||
|
#define ALGUE_VERBOSE(data) ALGUE_BASE(6, data)
|
||||||
|
#define ALGUE_TODO(data) ALGUE_BASE(4, "TODO : " << data)
|
||||||
|
#else
|
||||||
|
#define ALGUE_INFO(data) do { } while(false)
|
||||||
|
#define ALGUE_DEBUG(data) do { } while(false)
|
||||||
|
#define ALGUE_VERBOSE(data) do { } while(false)
|
||||||
|
#define ALGUE_TODO(data) do { } while(false)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define ALGUE_ASSERT(cond,data) \
|
||||||
|
do { \
|
||||||
|
if (!(cond)) { \
|
||||||
|
JSON_CRITICAL(data); \
|
||||||
|
assert(!#cond); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
17
algue/md5.cpp
Normal file
17
algue/md5.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#include <etk/types.h>
|
||||||
|
#include <algue/md5.h>
|
||||||
|
#include <openssl/md5.h>
|
||||||
|
|
||||||
|
std::vector<uint8_t> algue::md5::encode(const uint8_t* _data, int32_t _len) {
|
||||||
|
std::vector<uint8_t> out;
|
||||||
|
out.resize(MD5_DIGEST_LENGTH);
|
||||||
|
MD5(_data, _len, &out[0]);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
20
algue/md5.h
Normal file
20
algue/md5.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <etk/types.h>
|
||||||
|
|
||||||
|
namespace algue {
|
||||||
|
namespace md5 {
|
||||||
|
std::vector<uint8_t> encode(const uint8_t* _data, int32_t _len);
|
||||||
|
inline std::vector<uint8_t> encode(const std::vector<uint8_t>& _data) {
|
||||||
|
return algue::md5::encode(&_data[0], _data.size());
|
||||||
|
}
|
||||||
|
inline std::vector<uint8_t> encode(const std::string& _data) {
|
||||||
|
return algue::md5::encode(reinterpret_cast<const uint8_t*>(&_data[0]), _data.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
algue/sha1.cpp
Normal file
17
algue/sha1.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#include <etk/types.h>
|
||||||
|
#include <algue/sha1.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
|
std::vector<uint8_t> algue::sha1::encode(const uint8_t* _data, int32_t _len) {
|
||||||
|
std::vector<uint8_t> out;
|
||||||
|
out.resize(SHA_DIGEST_LENGTH);
|
||||||
|
SHA1(_data, _len, &out[0]);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
20
algue/sha1.h
Normal file
20
algue/sha1.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/** @file
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <etk/types.h>
|
||||||
|
|
||||||
|
namespace algue {
|
||||||
|
namespace sha1 {
|
||||||
|
std::vector<uint8_t> encode(const uint8_t* _data, int32_t _len);
|
||||||
|
inline std::vector<uint8_t> encode(const std::vector<uint8_t>& _data) {
|
||||||
|
return algue::sha1::encode(&_data[0], _data.size());
|
||||||
|
}
|
||||||
|
inline std::vector<uint8_t> encode(const std::string& _data) {
|
||||||
|
return algue::sha1::encode(reinterpret_cast<const uint8_t*>(&_data[0]), _data.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
45
lutin_algue.py
Normal file
45
lutin_algue.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
import lutin.module as module
|
||||||
|
import lutin.tools as tools
|
||||||
|
|
||||||
|
|
||||||
|
def get_type():
|
||||||
|
return "LIBRARY"
|
||||||
|
|
||||||
|
def get_desc():
|
||||||
|
return "Algorithm generic"
|
||||||
|
|
||||||
|
def get_licence():
|
||||||
|
return "APACHE-2"
|
||||||
|
|
||||||
|
def get_compagny_type():
|
||||||
|
return "com"
|
||||||
|
|
||||||
|
def get_compagny_name():
|
||||||
|
return "atria-soft"
|
||||||
|
|
||||||
|
def get_maintainer():
|
||||||
|
return ["Mr DUPIN Edouard <yui.heero@gmail.com>"]
|
||||||
|
|
||||||
|
def get_version():
|
||||||
|
return [0,1,"dev"]
|
||||||
|
|
||||||
|
def create(target, module_name):
|
||||||
|
my_module = module.Module(__file__, module_name, get_type())
|
||||||
|
my_module.add_module_depend(['elog', 'etk', 'crypto'])
|
||||||
|
my_module.add_extra_compile_flags()
|
||||||
|
my_module.add_src_file([
|
||||||
|
'algue/debug.cpp',
|
||||||
|
'algue/base64.cpp',
|
||||||
|
'algue/sha1.cpp',
|
||||||
|
'algue/md5.cpp'
|
||||||
|
])
|
||||||
|
my_module.add_header_file([
|
||||||
|
'algue/base64.h',
|
||||||
|
'algue/sha1.h',
|
||||||
|
'algue/md5.h',
|
||||||
|
])
|
||||||
|
my_module.add_path(tools.get_current_path(__file__))
|
||||||
|
return my_module
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user