コード例 #1
0
        private static bool Hmac(
            IntPtr key,
            IntPtr @in,
            IntPtr @out,
            IntPtr statusPtr,
            int bitness)
        {
            using (var status = new Status(StatusSafeHandle.FromIntPtr(statusPtr)))
            {
                try
                {
                    var keyBinary = new Binary(BinarySafeHandle.FromIntPtr(key));
                    var inBinary  = new Binary(BinarySafeHandle.FromIntPtr(@in));
                    var outBinary = new Binary(BinarySafeHandle.FromIntPtr(@out));

                    var keyBytes = keyBinary.ToArray();
                    var inBytes  = inBinary.ToArray();

                    var outBytes = CalculateHash(keyBytes, inBytes, bitness: bitness);
                    outBinary.WriteBytes(outBytes);

                    return(true);
                }
                catch (Exception ex)
                {
                    // let mongocrypt level to handle the error
                    status.SetStatus(1, ex.Message);
                    return(false);
                }
            }
        }
コード例 #2
0
        public static bool RsaSign(
            IntPtr ctx,
            IntPtr key,
            IntPtr inData,
            IntPtr outData,
            IntPtr statusPtr)
        {
            using (var status = new Status(StatusSafeHandle.FromIntPtr(statusPtr)))
            {
                try
                {
                    var keyBinary   = new Binary(BinarySafeHandle.FromIntPtr(key));
                    var inputBinary = new Binary(BinarySafeHandle.FromIntPtr(inData));
                    var outBinary   = new Binary(BinarySafeHandle.FromIntPtr(outData));

                    byte[] inputBytes = inputBinary.ToArray();
                    byte[] keyBytes   = keyBinary.ToArray();

                    // Hash and sign the data.
                    var signedData = HashAndSignBytes(inputBytes, keyBytes);

                    outBinary.WriteBytes(signedData);

                    return(true);
                }
                catch (Exception e)
                {
                    // let mongocrypt level to handle the error
                    status.SetStatus(1, e.Message);
                    return(false);
                }
            }
        }
コード例 #3
0
 public static bool GenerateRandom(
     IntPtr ctx,
     IntPtr @out,
     uint count,
     IntPtr statusPtr)
 {
     using (var outBinary = new Binary(BinarySafeHandle.FromIntPtr(@out)))
         using (var status = new Status(StatusSafeHandle.FromIntPtr(statusPtr)))
         {
             try
             {
                 using (var randomNumberGenerator = RandomNumberGenerator.Create())
                 {
                     var bytes = new byte[count];
                     randomNumberGenerator.GetBytes(bytes);
                     outBinary.WriteBytes(bytes);
                     return(true);
                 }
             }
             catch (Exception e)
             {
                 status.SetStatus(1, e.Message);
                 return(false);
             }
         }
 }
コード例 #4
0
        public static bool Decrypt(
            IntPtr ctx,
            IntPtr key,
            IntPtr iv,
            IntPtr @in,
            IntPtr @out,
            ref uint bytes_written,
            IntPtr statusPtr)
        {
            using (var status = new Status(StatusSafeHandle.FromIntPtr(statusPtr)))
            {
                try
                {
                    var keyBinary    = new Binary(BinarySafeHandle.FromIntPtr(key));
                    var inputBinary  = new Binary(BinarySafeHandle.FromIntPtr(@in));
                    var outputBinary = new Binary(BinarySafeHandle.FromIntPtr(@out));
                    var ivBinary     = new Binary(BinarySafeHandle.FromIntPtr(iv));

                    byte[] keyBytes   = keyBinary.ToArray();
                    byte[] ivBytes    = ivBinary.ToArray();
                    byte[] inputBytes = inputBinary.ToArray();

                    var outputBytes = AesCrypt(keyBytes, ivBytes, inputBytes, CryptMode.Decrypt);
                    bytes_written = (uint)outputBytes.Length;
                    outputBinary.WriteBytes(outputBytes);

                    return(true);
                }
                catch (Exception e)
                {
                    status.SetStatus(1, e.Message);
                    return(false);
                }
            }
        }
コード例 #5
0
        public static bool Hash(
            IntPtr ctx,
            IntPtr @in,
            IntPtr @out,
            IntPtr statusPtr)
        {
            using (var status = new Status(StatusSafeHandle.FromIntPtr(statusPtr)))
            {
                try
                {
                    var inputBinary = new Binary(BinarySafeHandle.FromIntPtr(@in));
                    var outBinary   = new Binary(BinarySafeHandle.FromIntPtr(@out));

                    var outBytes = CalculateHash(inputBinary.ToArray());
                    outBinary.WriteBytes(outBytes);
                    return(true);
                }
                catch (Exception ex)
                {
                    status.SetStatus(1, ex.Message);
                    return(false);
                }
            }
        }