documentation for PICrypt

git-svn-id: svn://db.shs.com.ru/pip@102 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2015-04-15 14:23:00 +00:00
parent 85d90729fa
commit 9435ef726b
4 changed files with 59 additions and 9 deletions

View File

@@ -115,14 +115,6 @@ public:
}; };
class Parent { class Parent {
public: public:
virtual void print() {piCout << "Parent";} virtual void print() {piCout << "Parent";}
@@ -144,6 +136,7 @@ public:
//#include "unicode/uchar.h" //#include "unicode/uchar.h"
#include "picrypt.h" #include "picrypt.h"
#include "pifixedpoint.h"
int main (int argc, char * argv[]) { int main (int argc, char * argv[]) {
PICrypt cr; PICrypt cr;
@@ -169,7 +162,7 @@ int main (int argc, char * argv[]) {
return 0; return 0;
// //char uc[] = "←↑→↓АБВ"; // //char uc[] = "←↑→↓АБВ";
// char uc[] = "│─┌┐└┘├┤┬┴┼"; // char uc[] = "│─┌┐└┘├┤┬┴┼";
// ─────────────┴─┴┴──┴┴┴─┘
// PIString us = PIString::fromUTF8(uc); // PIString us = PIString::fromUTF8(uc);
// //piForeachC (PIChar & c, us) // //piForeachC (PIChar & c, us)

View File

@@ -1,3 +1,25 @@
/*! \file pibasetransfer.h
* \brief Base class for reliable send and receive data in fixed packets with error correction, pause and resume
*/
/*
PIP - Platform Independent Primitives
Base class for reliable send and receive data in fixed packets with error correction, pause and resume
Copyright (C) 2015 Andrey Bychkov work.a.b@yandex.ru
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
(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 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/>.
*/
#ifndef PIBASETRANSFER_H #ifndef PIBASETRANSFER_H
#define PIBASETRANSFER_H #define PIBASETRANSFER_H

View File

@@ -21,6 +21,8 @@
#ifdef PIP_CRYPT #ifdef PIP_CRYPT
# include "sodium.h" # include "sodium.h"
#endif #endif
const char hash_def_key[] = "_picrypt_"; const char hash_def_key[] = "_picrypt_";
@@ -144,6 +146,17 @@ PIByteArray PICrypt::hash(const PIString & secret) {
} }
PIByteArray PICrypt::generateKey() {
PIByteArray hash;
#ifdef PIP_CRYPT
sodium_init();
hash.resize(crypto_secretbox_KEYBYTES);
randombytes_buf(hash.data(), hash.size());
#endif
return hash;
}
int PICrypt::sizeKey() { int PICrypt::sizeKey() {
#ifdef PIP_CRYPT #ifdef PIP_CRYPT
return crypto_secretbox_KEYBYTES; return crypto_secretbox_KEYBYTES;

View File

@@ -27,18 +27,40 @@
class PICrypt { class PICrypt {
public: public:
//! Construct and generate random key
PICrypt(); PICrypt();
//! Set key to "key", key size must be a \a sizeKey()
bool setKey(const PIByteArray & key); bool setKey(const PIByteArray & key);
//! Generate and set key from keyphrase "secret"
PIByteArray setKey(const PIString & secret); PIByteArray setKey(const PIString & secret);
//! Returns current key
PIByteArray key() {return key_;} PIByteArray key() {return key_;}
//! Encrypt given data "data", result size will be increased by \a sizeCrypt()
PIByteArray crypt(const PIByteArray & data); PIByteArray crypt(const PIByteArray & data);
//! Decrypt given data "crypt_data"
PIByteArray decrypt(const PIByteArray & crypt_data, bool * ok = 0); PIByteArray decrypt(const PIByteArray & crypt_data, bool * ok = 0);
//! Encrypt given data "data" with key "key", result size will be increased by \a sizeCrypt()
static PIByteArray crypt(const PIByteArray & data, const PIByteArray & key); static PIByteArray crypt(const PIByteArray & data, const PIByteArray & key);
//! Decrypt given data "crypt_data" with key "key"
static PIByteArray decrypt(const PIByteArray & crypt_data, const PIByteArray & key, bool * ok = 0); static PIByteArray decrypt(const PIByteArray & crypt_data, const PIByteArray & key, bool * ok = 0);
//! Generate hash from keyphrase "secret", may be used as a key for encryption
static PIByteArray hash(const PIString & secret); static PIByteArray hash(const PIString & secret);
//! Generate random key
static PIByteArray generateKey();
//! Returns key size
static int sizeKey(); static int sizeKey();
//! Returns size which be added to data size in encryption process
static int sizeCrypt(); static int sizeCrypt();
private: private: