首页 > 代码库 > 自己封装的openssl+国密算法的C++接口
自己封装的openssl+国密算法的C++接口
Digest
#ifndef _DIGESTCALC_H#define _DIGESTCALC_H/**********************************/* 使用示例:(sm3算法)/* DigestCalc hashcl;/* hashcl.Init(DigestCalc::alg_id::sm3);/* hashcl.Update(in,inl);/* vector<unsigned int> out(hashcl.GetHashLength());/* hashcl.Final(out.data());/**********************************/class _DigestCalc;class DigestCalc{public: class alg_id { friend class DigestCalc; explicit alg_id(int i):_id(i){} int _id; public: static const alg_id sm3; static const alg_id sha; static const alg_id sha1; static const alg_id sha224; static const alg_id sha256; static const alg_id sha384; static const alg_id sha512; static const alg_id md2; static const alg_id md4; static const alg_id md5; static const alg_id rc2; static const alg_id rc4; static const alg_id rc5; static const alg_id dsa; static const alg_id mdc2; static const alg_id ecdsa; }; public: DigestCalc(); ~DigestCalc(); //@param alg 算法标识 //@return 出错返回false,成功返回true bool Init(alg_id id); //@param in 输入缓冲区地址 //@param inl 输入数据长度 //@return 出错返回false,成功返回true bool Update(const unsigned char *in,unsigned int inl);//增加被计算数据。 //@param out 输出缓冲区地址 //@return 出错返回false,成功返回true bool Final(unsigned char *out);//计算出哈希结果,存放在out指向的缓冲区中。 int GetHashLength()const;//在调用Final前先调用此函数获取哈希长度,保证哈希的缓冲区够长。private: _DigestCalc *_impl; DigestCalc(const DigestCalc &); DigestCalc &operator=(const DigestCalc &);};#endif
Cipher
#ifndef _CIPHERCALC_H#define _CIPHERCALC_H/**********************************/* 使用示例:(sm4算法cbc模式加密)/* CipherCalc cipher;/* outl=0;/* cipher.Init(CipherCalc::alg_id::sm4_cbc,key,iv,CipherCalc::ENCRYPT,CipherCalc::PADMODE::OPENSSL_PADDING);/* cipher.Update(in,inl,out,&outl);/* cipher.Final(out+outl,&outl);/**********************************/class _CipherCalc;class CipherCalc{public: //表示进行加密运算还是解密运算 class ENCMODE { friend class CipherCalc; int enc; explicit ENCMODE(int x):enc(x){} public: static const ENCMODE ENCRYPT;//加密 static const ENCMODE DECRYPT;//解密 }; //表示数据的填充方式 class PADMODE { friend class CipherCalc; int padding; explicit PADMODE(int x):padding(x){} public: static const PADMODE NO_PADDING;//用户自己填充数据,保证数据长度为分组长度的整数倍,否则运算出错。 static const PADMODE OPENSSL_PADDING;//openssl填充算法,填充数据至分组长度的整数倍。如果数据本身的长度就是分组 //长度的整数倍,就追加一个分组。填充的所有字节值都等于追加的数据的长度。 }; //表示使用哪种对称算法 class alg_id { friend class CipherCalc; explicit alg_id(int id):_id(id){} int _id; public: static const alg_id sm4_ecb;//国密SM4 ECB模式 static const alg_id sm4_cbc;//国密SM4 CBC模式 static const alg_id des_cfb; static const alg_id des_cfb1; static const alg_id des_cfb8; static const alg_id des_ede_cfb; static const alg_id des_ede3_cfb; static const alg_id des_ede3_cfb1; static const alg_id des_ede3_cfb8; static const alg_id des_ofb; static const alg_id des_ede_ofb; static const alg_id des_ede3_ofb; static const alg_id desx_cbc; static const alg_id des_cbc; //单DES CBC模式 static const alg_id des_ede_cbc; //3DES CBC模式 static const alg_id des_ede3_cbc; static const alg_id des_ecb; //单DES ECB模式 static const alg_id des_ede; static const alg_id des_ede3; //3DES ECB模式 static const alg_id rc4; static const alg_id rc4_40; static const alg_id rc4_hmac_md5; static const alg_id idea_ecb; static const alg_id idea_cfb; static const alg_id idea_ofb; static const alg_id idea_cbc; static const alg_id seed_ecb; static const alg_id seed_cfb; static const alg_id seed_ofb; static const alg_id seed_cbc; static const alg_id rc2_ecb; static const alg_id rc2_cfb; static const alg_id rc2_ofb; static const alg_id rc2_cbc; static const alg_id rc2_40_cbc; static const alg_id rc2_64_cbc; static const alg_id bf_ecb; static const alg_id bf_cfb; static const alg_id bf_ofb; static const alg_id bf_cbc; static const alg_id cast5_ecb; static const alg_id cast5_cfb; static const alg_id cast5_ofb; static const alg_id cast5_cbc; static const alg_id rc5_32_12_16_ecb; static const alg_id rc5_32_12_16_cfb; static const alg_id rc5_32_12_16_ofb; static const alg_id rc5_32_12_16_cbc; static const alg_id aes_128_ecb; static const alg_id aes_128_cbc; static const alg_id aes_128_cfb; static const alg_id aes_128_cfb1; static const alg_id aes_128_cfb8; static const alg_id aes_128_ofb; static const alg_id aes_128_ctr; static const alg_id aes_128_gcm; static const alg_id aes_128_xts; static const alg_id aes_192_ecb; static const alg_id aes_192_cbc; static const alg_id aes_192_cfb; static const alg_id aes_192_cfb1; static const alg_id aes_192_cfb8; static const alg_id aes_192_ofb; static const alg_id aes_192_ctr; static const alg_id aes_192_gcm; static const alg_id aes_256_ecb; static const alg_id aes_256_cbc; static const alg_id aes_256_cfb; static const alg_id aes_256_cfb1; static const alg_id aes_256_cfb8; static const alg_id aes_256_ofb; static const alg_id aes_256_ctr; static const alg_id aes_256_gcm; static const alg_id aes_256_xts; static const alg_id camellia_128_ecb ; static const alg_id camellia_128_cbc ; static const alg_id camellia_128_cfb ; static const alg_id camellia_128_cfb1; static const alg_id camellia_128_cfb8; static const alg_id camellia_128_ofb ; static const alg_id camellia_192_ecb ; static const alg_id camellia_192_cbc ; static const alg_id camellia_192_cfb ; static const alg_id camellia_192_cfb1; static const alg_id camellia_192_cfb8; static const alg_id camellia_192_ofb ; static const alg_id camellia_256_ecb ; static const alg_id camellia_256_cbc ; static const alg_id camellia_256_cfb ; static const alg_id camellia_256_cfb1; static const alg_id camellia_256_cfb8; static const alg_id camellia_256_ofb ; };public: CipherCalc(); ~CipherCalc(void); //@param alg 算法标识 //@param key 密钥 //@param iv cbc算法初始向量,如果iv==NULL,则表示iv值为全0。 //@param enc ENCRYPT表示加密运算,DECRYPT表示解密运算 //@param padding 数据填充方式,默认为用户自己填充,即CipherCalc不对输入数据做任何修正。 //@return 出错返回false,成功返回true bool Init(alg_id alg,unsigned char *key,const unsigned char *iv, ENCMODE enc ,PADMODE padding = PADMODE::NO_PADDING); //@param in 输入缓冲区地址 //@param inl 输入数据长度 //@param out 输出缓冲区地址 //@param outl 返回 *outl+输出数据长度 //@return 出错返回false,成功返回true bool Update(const unsigned char *in,int inl,unsigned char *out,int *outl); //@param out 输出缓冲区地址 //@param outl 返回 *outl+输出数据长度 //@return 出错返回false,成功返回true bool Final(unsigned char *out,int *outl);private: _CipherCalc *_impl; CipherCalc(const CipherCalc &); CipherCalc &operator=(const CipherCalc &);};#endif
自己封装的openssl+国密算法的C++接口
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。