Skip to content

Instantly share code, notes, and snippets.

View VictorZhang2014's full-sized avatar

Victor VictorZhang2014

View GitHub Profile
@VictorZhang2014
VictorZhang2014 / HmacSHA256 Encryption
Last active November 20, 2016 11:27
iOS/C Hmac+SHA256 Encryption
#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>
@interface ZRCommonCryption : NSObject
/**
* 加密方式,MAC算法: HmacSHA256
*
* @param plaintext 要加密的文本
* @param key 秘钥
@VictorZhang2014
VictorZhang2014 / URLEncode-Decode
Created November 20, 2016 11:33
C URLEncode/Decode
/**
* @brief URLEncode 对字符串URL编码
*
* @param str 原字符串
* @param strSize 原字符串长度(不包括最后的\0)
* @param result 结果缓冲区的地址
* @param resultSize 结果缓冲区的大小(包括最后的\0)
*
* @return: >0:resultstring 里实际有效的长度
@VictorZhang2014
VictorZhang2014 / zlib-(de)compress(es)-file-content
Created December 18, 2016 14:44
zlib de/compress(es) file content rather than string
#include <stdio.h>
#include <zlib.h>
/*
* A good example for using zlib is correlated de/compress file instead of string
*/
void decompress_one_file(char *infilename, char *outfilename);
void compress_one_file(char *infilename, char *outfilename);
@VictorZhang2014
VictorZhang2014 / Compress-by-zlib
Created December 18, 2016 15:25
Compressing From string to string by zlib. Imported zlib.tbd directly if you're on the MAC.
bool CompressByGZIP(std::string & uncompressedStr, std::string & compressedStr)
{
if (uncompressedStr.length() <= 0) {
return false;
}
compressedStr.resize(uncompressedStr.size() * 1.5 + 12);
/* Before we can begin compressing (aka "deflating") data using the zlib
functions, we must initialize zlib. Normally this is done by calling the
@VictorZhang2014
VictorZhang2014 / BubbleIntersection
Created January 14, 2017 05:01
Bubble Intersection in iOS
@interface MyView : UIView
@property (nonatomic, assign) BOOL isMySelf;
@end
@implementation MyView
- (instancetype)init
{
@VictorZhang2014
VictorZhang2014 / UTF8EncodingCPP
Created March 19, 2017 02:34
UTF8 Encoding was written by c++
#include <vector>
#include <codecvt>
std::string VLUtilitiesEncoding::UTF8Encoding(std::string & unencodedStr)
{
if (unencodedStr.length() <= 0) return "";
std::wstring_convert<std::codecvt_utf8<wchar_t>,wchar_t> conversion;
std::wstring wbodyStr = conversion.from_bytes(unencodedStr);
return conversion.to_bytes(wbodyStr);
@VictorZhang2014
VictorZhang2014 / DecompressCPP
Created March 19, 2017 02:36
Decompress/compress std::string methods was written by C++
#include <zlib.h>
#include <sstream>
/** Compress a STL string using zlib with given compression level and return
* the binary data.
https://panthema.net/2007/0328-ZLibString.html
*/
std::string VLUtilitiesEncoding::compress(const std::string & str, int compressionlevel = Z_BEST_COMPRESSION)
{
z_stream zs; // z_stream is zlib's control structure
@VictorZhang2014
VictorZhang2014 / RSACryptoGraphy.cs
Created March 29, 2017 11:22
RSACryptoGraphy, a set of methods was written by C#.
/// <summary>
/// Create public key/private key file, actually, the original certificates was modified to a XML file
/// 创建公钥/私钥文件,其实就是把原有的证书的内容修改成XML格式的
/// </summary>
public static void CreateCertificateKeyXML(string path, string key)
{
try
{
FileStream keyxml = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(keyxml);
@VictorZhang2014
VictorZhang2014 / RSACryptography.cs
Last active March 31, 2017 06:50
RSACryptography, a set of RSA de/encryt methods who can read the p12 file with a password , and can generate public key in terms of its private key. It was written by C#.
//This file is encrypt/decrypt data in a default way which can not decrypt/decrypt a chunk of data
using System;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace RSA_CSharp_Example
{
class Program
@VictorZhang2014
VictorZhang2014 / RSACryptography1.h
Last active March 31, 2017 11:28
RSACryptography, a set of RSA encrypt/decrypt methods for a chunk of data in Objective-c/iOS
#import <Foundation/Foundation.h>
@interface RSACryptographic : NSObject
- (void)loadPublicKeyFromFile:(NSString*)derFilePath;
- (void)loadPublicKeyFromData:(NSData*)derData;
- (void)loadPrivateKeyFromFile:(NSString*)p12FilePath password:(NSString*)p12Password;
- (void)loadPrivateKeyFromData:(NSData*)p12Data password:(NSString*)p12Password;