#可信 #算法 #外部 #rsa #函数 #面向 #服务

performant_encryption

这是一个提供高效且可信加密功能的Rust库

6个版本

0.1.6 2023年6月21日
0.1.5 2023年6月5日
0.1.3 2023年2月19日
0.1.1 2022年11月21日

#1556加密学

每月 36 次下载

Apache-2.0

51KB
1K SLoC

PerformantEncryption

GitHub issues GitHub

此仓库用于将Rust Crypto加密算法暴露给NETCore-API以用于加密API服务。

此crate的首页可以在这里找到:这里.

C# 使用示例

BCrypt

public class BcryptWrapper
    {
        [DllImport("performant_encryption.dll")]
        private static extern IntPtr bcrypt_hash(string passToHash);

        [DllImport("performant_encryption.dll")]
        private static extern bool bcrypt_verify(string password, string hash);
        [DllImport("performant_encryption.dll")]
        public static extern void free_cstring(IntPtr stringToFree);

        public IntPtr HashPassword(string passwordToHash)
        {
            return bcrypt_hash(passwordToHash);
        }

        public async Task<IntPtr> HashPasswordAsync(string passwordToHash)
        {
            return await Task.Run(() =>
            {
                return bcrypt_hash(passwordToHash);
            });
        }
        public bool Verify(string hashedPassword, string unhashed)
        {
            return bcrypt_verify(unhashed, hashedPassword);
        }
        public async Task<bool> VerifyAsync(string hashedPassword, string unhashed)
        {
            return await Task.Run(() =>
            {
                return bcrypt_verify(unhashed, hashedPassword);
            });
        }
    }

RSA

public class RustRSAWrapper
    {
        public struct RustRsaKeyPair
        {
            public IntPtr pub_key;
            public IntPtr priv_key;
        }
        public struct RsaSignResult
        {
            public IntPtr signature;
            public IntPtr public_key;
        }

        [DllImport("performant_encryption.dll")]
        private static extern RustRsaKeyPair get_key_pair(int key_size);
        [DllImport("performant_encryption.dll")]
        private static extern IntPtr rsa_encrypt(string publicKey, string dataToEncrypt);
        [DllImport("performant_encryption.dll")]
        private static extern IntPtr rsa_decrypt(string privateKey, string dataToDecrypt);
        [DllImport("performant_encryption.dll")]
        private static extern RsaSignResult rsa_sign(string dataToSign, int keySize);
        [DllImport("performant_encryption.dll")]
        private static extern IntPtr rsa_sign_with_key(string privateKey, string dataToSign);
        [DllImport("performant_encryption.dll")]
        private static extern bool rsa_verify(string publicKey, string dataToVerify, string signature);
        [DllImport("performant_encryption.dll")]
        public static extern void free_cstring(IntPtr stringToFree);

        public IntPtr RsaSignWithKey(string privateKey, string dataToSign)
        {
            if (string.IsNullOrEmpty(privateKey))
            {
                throw new Exception("You must provide a private key to sign your data");
            }
            if (string.IsNullOrEmpty(dataToSign))
            {
                throw new Exception("You must provide data to sign with the private key");
            }
            return rsa_sign_with_key(privateKey, dataToSign);
        }
        public async Task<IntPtr> RsaSignWithKeyAsync(string privateKey, string dataToSign)
        {
            if (string.IsNullOrEmpty(privateKey))
            {
                throw new Exception("You must provide a private key to sign your data");
            }
            if (string.IsNullOrEmpty(dataToSign))
            {
                throw new Exception("You must provide data to sign with the private key");
            }
            return await Task.Run(() =>
            {
                return rsa_sign_with_key(privateKey, dataToSign);
            });
        }
        public async Task<bool> RsaVerifyAsync(string publicKey, string dataToVerify, string signature)
        {
            if (string.IsNullOrEmpty(publicKey))
            {
                throw new Exception("You must provide a public key to verify the rsa signature");
            }
            if (string.IsNullOrEmpty(dataToVerify))
            {
                throw new Exception("You must provide the original data to verify the rsa signature");
            }
            if (string.IsNullOrEmpty(dataToVerify))
            {
                throw new Exception("You must provide that digital signature that was provided by our signing");
            }
            return await Task.Run(() =>
            {
                return rsa_verify(publicKey, dataToVerify, signature);
            });
        }
        public bool RsaVerify(string publicKey, string dataToVerify, string signature)
        {
            if (string.IsNullOrEmpty(publicKey))
            {
                throw new Exception("You must provide a public key to verify the rsa signature");
            }
            if (string.IsNullOrEmpty(dataToVerify))
            {
                throw new Exception("You must provide the original data to verify the rsa signature");
            }
            if (string.IsNullOrEmpty(dataToVerify))
            {
                throw new Exception("You must provide that digital signature that was provided by our signing");
            }
            return rsa_verify(publicKey, dataToVerify, signature);
        }

        public RsaSignResult RsaSign(string dataToSign, int keySize)
        {
            if (string.IsNullOrEmpty(dataToSign))
            {
                throw new Exception("You must provide data to sign with RSA");
            }
            if (keySize != 1024 && keySize != 2048 && keySize != 4096)
            {
                throw new Exception("You must provide a valid key bit size to sign with RSA");
            }
            return rsa_sign(dataToSign, keySize);
        }

        public async Task<RsaSignResult> RsaSignAsync(string dataToSign, int keySize)
        {
            if (string.IsNullOrEmpty(dataToSign))
            {
                throw new Exception("You must provide data to sign with RSA");
            }
            if (keySize != 1024 && keySize != 2048 && keySize != 4096)
            {
                throw new Exception("You must provide a valid key bit size to sign with RSA");
            }
            return await Task.Run(() =>
            {
                return rsa_sign(dataToSign, keySize);

            });
        }
        public IntPtr RsaDecrypt(string privateKey, string dataToDecrypt)
        {
            if (string.IsNullOrEmpty(privateKey) || string.IsNullOrEmpty(dataToDecrypt))
            {
                throw new Exception("You need to provide a private key and data to decrypt to use RsaCrypt");
            }
            return rsa_decrypt(privateKey, dataToDecrypt);
        }
        public async Task<IntPtr> RsaDecryptAsync(string privateKey, string dataToDecrypt)
        {
            if (string.IsNullOrEmpty(privateKey) || string.IsNullOrEmpty(dataToDecrypt))
            {
                throw new Exception("You need to provide a private key and data to decrypt to use RsaCrypt");
            }
            return await Task.Run(() =>
            {
                return rsa_decrypt(privateKey, dataToDecrypt);
            });
        }
        public IntPtr RsaEncrypt(string publicKey, string dataToEncrypt)
        {
            if (string.IsNullOrEmpty(publicKey) || string.IsNullOrEmpty(dataToEncrypt))
            {
                throw new Exception("You need to provide a public key and data to encrypt to use RsaEncrypt");
            }
            return rsa_encrypt(publicKey, dataToEncrypt);
        }
        public async Task<IntPtr> RsaEncryptAsync(string publicKey, string dataToEncrypt)
        {
            if (string.IsNullOrEmpty(publicKey) || string.IsNullOrEmpty(dataToEncrypt))
            {
                throw new Exception("You need to provide a public key and data to encrypt to use RsaEncrypt");
            }
            return await Task.Run(() =>
            {
                return rsa_encrypt(publicKey, dataToEncrypt);
            });
        }

        public RustRsaKeyPair GetKeyPair(int keySize)
        {
            if (keySize != 1024 && keySize != 2048 && keySize != 4096)
            {
                throw new Exception("Please pass in a valid key size.");
            }
            return get_key_pair(keySize);
        }
        public async Task<RustRsaKeyPair> GetKeyPairAsync(int keySize)
        {
            if (keySize != 1024 && keySize != 2048 && keySize != 4096)
            {
                throw new Exception("Please pass in a valid key size.");
            }
            return await Task.Run(() =>
            {
                return get_key_pair(keySize);
            });
        }
    }

支持者/捐赠

Microsoft For Startups

请我喝咖啡

此项目通过Apache 2.0维护。

依赖项

~11MB
~192K SLoC