📄 algorithm.h
字号:
/************************************************************************* * * * 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 2 of the License, or * * (at your option) any later version. * * * *************************************************************************/#ifndef _ALGORITHM_H__#define _ALGORITHM_H__#include "global.h"#include "exception.h"#include "i18n.h"#include <strings.h>#include <unistd.h>#include <arpa/inet.h>#include <netdb.h>#include <sys/socket.h>#include <netinet/in.h>#include <errno.h>#include <string>#include <map>#include <algorithm>#include <vector>#include <openssl/md5.h>#include <openssl/sha.h>#include <openssl/hmac.h>using namespace std;/** * This class provides some convenient methods for the following * algorithms: BASE64, BASE64_NIL, hmac_md5 and hmac_sha1. * * @author Timo Benk <t_benk@web.de> */class Algorithm{ public: enum CODEC { BASE64, BASE64_NIL }; /** * Encode str with the given codec. * * @param str The string that should be encoded. * @param codec The codec that should be used. * @returns The encoded string. */ static string encode (const string &str, CODEC codec); /** * Decode str with the given codec. * * @param str The string that should be decoded. * @param codec The codec that should be used. * @returns The encoded string. * * @throws DecodeException * If the BASE64 data was invalid. */ static string decode (const string &str, CODEC codec); /** * Calculates the keyed md5sum. (rfc2104) * * @param text The text that should be checksummed. * @param key The key that should be used. * @param digest The checksum that is calculated out of key * and text will be stored at digest. digest * needs to be at least 16 Bytes big. */ static void hmac_md5 ( const string &text, const string &key, unsigned char * digest ); /** * Calculates the keyed md5sum and returns the digest as a * string. (rfc2104) * Just for your convenience. * * @param str The text that should be checksummed. * @param key The key that should be used. * @returns A string representation of the checksum * that was calculated using str and key. */ static string hmac_md5_str ( const string &str, const string &key ); /** * Calculates the keyed sha1sum. (rfc2104) * * @param text The text that should be checksummed. * @param key The key that should be used. * @param digest The checksum that is calculated out of key * and text will be stored at digest. digest * needs to be at least 20 Bytes big. */ static void hmac_sha1 ( const string &text, const string &key, unsigned char * digest ); /** * Calculates the keyed sha1sum and returns the digest as a * string. (rfc2104) * Just for your convenience. * * @param str The text that should be checksummed. * @param key The key that should be used. * @returns A string representation of the checksum * that was calculated using str and key. */ static string hmac_sha1_str (const string &str, const string &key); protected: /** * The base64 character table. */ static char tbl_base64[]; /** * Encode orig_str with the base64 codec and store * the result in the string pointed by enc_str. * * @param orig_str The string that should be encoded. * @param enc_str The resulting encoded string will be stored * at the address pointed to by enc_str. */ static void enc_base64 (const string &orig_str, string * enc_str); /** * Decode enc_str with the base64 codec and store * the result in the string pointed by orig_str. * If successfull 0 is returned, and -1 if the * base64 data was invalid. * * @param enc_str The string that should be decoded. * @param orig_str The resulting decoded string will be stored * at the address pointed to by orig_str. * @returns 0 if the decoding process was successfull and -1 if * the base64 data was invalid. */ static int dec_base64 (const string &enc_str, string * orig_str); /** * Decode enc_str with a slightly advanced base64 * codec and store the result in the string pointed * by orig_str. * Each zero byte will be substituted with the * sequence "<NIL>". * If successfull 0 is returned, and -1 if the * base64 data was invalid. * * @param enc_str The string that should be decoded. * @param orig_str The resulting decoded string will be stored * at the address pointed to by orig_str. * @returns 0 if the decodeing process was successfull and -1 if * the base64 data was invalid. */ static int dec_base64_nil (const string &enc_str, string * orig_str); /** * Encode enc_str with a slightly advanced base64 codec * and store the result in the string pointed by orig_str. * The sequence "<NIL>" will be substituted with one zero byte. * * @param orig_str The string that should be encoded. * @param enc_str The resulting encoded string will be stored * at the address pointed to by enc_str. */ static void enc_base64_nil (string orig_str, string * enc_str);};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -