/// <summary> /// /// </summary> /// <param name="SourceBuffer"></param> /// <param name="Length"></param> /// <param name="Challenge"></param> /// <param name="ExpectedResponse"></param> /// <param name="Password"></param> /// <returns></returns> public static unsafe bool Decrypt(byte* SourceBuffer, int Length, uint Challenge, uint ExpectedResponse, byte[] Password) { bool isSuccessful = false; // init crush32 if (Crush32.CX_SUCCESS == Crush32.cxInit()) { if (Crush32.CX_SUCCESS == Crush32.cxBuf2BufInit()) { // pin the managed sourcebuffer in memory so crush32 can access // and we can directly use without marshaling. fixed (byte* ptrPassword = Password) { // set password && decrypt if (Crush32.CX_SUCCESS == Crush32.cxSetPassword(ptrPassword)) if (Crush32.CX_SUCCESS == Crush32.cxBufDecrypt(SourceBuffer, Length, Challenge, ExpectedResponse)) isSuccessful = true; } Crush32.cxBuf2BufClose(); } Crush32.cxCleanup(); } return isSuccessful; }
/// <summary> /// Fast encrypt data on unmanaged pointer /// </summary> /// <param name="SourceBuffer"></param> /// <param name="Length"></param> /// <param name="Challenge"></param> /// <param name="Password"></param> /// <returns></returns> public static unsafe uint Encrypt(byte* SourceBuffer, int Length, uint Challenge, byte[] Password) { uint expectedResponse = 0; // init crush32 if (Crush32.CX_SUCCESS == Crush32.cxInit()) { if (Crush32.CX_SUCCESS == Crush32.cxBuf2BufInit()) { // pin the managed sourcebuffer in memory so crush32 can access // and we can directly use without marshaling. fixed (byte* ptrPassword = Password) { // set password && encrypt if (Crush32.CX_SUCCESS == Crush32.cxSetPassword(ptrPassword)) expectedResponse = Crush32.cxBufEncrypt(SourceBuffer, Length, Challenge); } Crush32.cxBuf2BufClose(); } Crush32.cxCleanup(); } return expectedResponse; }
/// <summary> /// Fast compresses data from managed sourcebuffer to managed targetbuffer using pointers /// </summary> /// <param name="SourceBuffer">SourceBuffer containing data to be compressed</param> /// <param name="SourceIndex">Cursor in SourceBuffer to start reading</param> /// <param name="TargetBuffer">TargetBuffer for compressed data</param> /// <param name="TargetIndex">Cursor in TargetBuffer to start writing</param> /// <param name="UncompressedLength">Uncompressed data length in SourceBuffer</param> /// <returns>Compressed length</returns> public static int Compress(byte[] SourceBuffer, int SourceIndex, byte[] TargetBuffer, int TargetIndex, int UncompressedLength) { int compressedLength = 0; // init crush32 if (Crush32.CX_SUCCESS == Crush32.cxInit()) { if (Crush32.CX_SUCCESS == Crush32.cxBuf2BufInit()) { // pin the managed source & targetbuffers in memory so crush32 can access them // and we can directly use them without marshaling. fixed (byte* ptrSourceBuffer = SourceBuffer, ptrTargetBuffer = TargetBuffer) { // add the offsets to the pointers (they still point to the beginning of the buffer) byte* ptrSourceIndex = ptrSourceBuffer + SourceIndex; byte* ptrTargetIndex = ptrTargetBuffer + TargetIndex; // compress compressedLength = Crush32.cxBuf2BufCompress(ptrSourceIndex, ptrTargetIndex, UncompressedLength); } Crush32.cxBuf2BufClose(); } Crush32.cxCleanup(); } return compressedLength; }