gainer_crypto 0.0.6 copy "gainer_crypto: ^0.0.6" to clipboard
gainer_crypto: ^0.0.6 copied to clipboard

A Library that can produce Cryptographic hash or can encrypt and decrypt text with key.

GaiNeR Crypto #

This is a package developed by GaiNeR Technologies to generate Common Cryptographic Hash. Also It can encrypt or decrypt texts or String with some well known methods.

Features #

  • Cryptography

    • Common Hash

      • Create Hash String From Text
      • Create Hash Bytes From Text
      • Create Hash String From Chunk Of Texts
      • Create Hash Bytes From Chunk Of Texts
    • Common HMAC Hash

      • Create HMAC Hash String From Text
      • Create HMAC Hash Bytes From Text
      • Create HMAC Hash String From Chunk Of Texts
      • Create HMAC Hash Bytes From Chunk Of Texts
  • Advanced Cryptography

    • Advanced Hash

      • Create Hash String From Text by combining multiple methods
      • Create Hash Bytes From Text by combining multiple methods
      • Create Hash String From Chunk Of Texts by combining multiple methods
      • Create Hash Bytes From Chunk Of Texts by combining multiple methods
      • Create Hash String From Text by repeating one method multiple times
      • Create Hash Bytes From Text by repeating one method multiple times
      • Create Hash String From Chunk Of Texts by repeating one method multiple times
      • Create Hash Bytes From Chunk Of Texts by repeating one method multiple times
    • Advanced HMAC Hash

      • Create HMAC Hash String From Text by combining multiple methods
      • Create HMAC Hash Bytes From Text by combining multiple methods
      • Create HMAC Hash String From Chunk Of Texts by combining multiple methods
      • Create HMAC Hash Bytes From Chunk Of Texts by combining multiple methods
      • Create HMAC Hash String From Text by repeating one method multiple times
      • Create HMAC Hash Bytes From Text by repeating one method multiple times
      • Create HMAC Hash String From Chunk Of Texts by repeating one method multiple times
      • Create HMAC Hash Bytes From Chunk Of Texts by repeating one method multiple times
  • Key Generation For Encryption and Decryption

  • IV Generation For Encryption and Decryption

  • Encryption and Decryption `

    • AES
    • Fernet
    • Salsa20
    • RSA
    • RSA Sign and Verification

Getting started #

To use only the Common Cryptographic functions

import 'package:gainer_crypto/crypto.dart';

To use the Advanced Cryptographic functions

import 'package:gainer_crypto/crypto_advanced.dart';

To use the encryption decryption functions or the key generation

import 'package:gainer_crypto/encrypt_decrypt.dart';

To use all together

import 'package:gainer_crypto/gainer_crypto.dart';

Usage #

All functions from this library start with prefix gr. For eg.

grHashString(GRCryptoType type, String input);

Here grHashString is a function name.

All the class names and enum names start with prefix GR. For eg.

GRKey key = grKeyFromUtf8("32 Length Key...");
GRIV iv = grIVFromLength(16);
GRCryptoType hashType = GRCryptoType.md5;
GRCryptoTypeHMAC hmacHashType = GRCryptoTypeHMAC.hmacSHA1;

Here,

GRKey and GRIV are classes

GRCryptoType and "GRCryptoTypeHMAC" are enums

grKeyFromUtf8 and grIVFromLength are functions

Crypto #

Hash

We can encrypt an input text through a specific method (eg. SHA1, MD5 etc). The hash generated in this process, can't be decrypted. For example,

final sha1Hash = grHashString(GRCryptoType.sha1, "inputText");

Here you can see the function grHashString takes two parameters,

* hashType: Specific Hash method, which should be of type *GRCryptoType*
* input: A string input to be encrypted

This function will convert the input String to sha1 hash as the hashType provided as GRCryptoType.sha1

The supported values of GRCryptoType enum are,

* sha1
* sha224
* sha256
* sha384
* sha512
* sha512_224
* sha512_256
* md5
* md2
* md4
* ripemd128
* ripemd160
* ripemd256
* ripemd320
* sha3_224
* sha3_256
* sha3_384
* sha3_512
* keccak224
* keccak256
* keccak384
* keccak512
* tiger
* whirlpool
* sm3

You can find more examples of Hashing in the Example Tab

HMAC

We can encrypt an input text through a specific method (eg. SHA1, MD5 etc) and a key. Using the key to generate hash is called HMAC. The hash generated in this process, can't be decrypted. For example,

const input = "inputText";
const key = "Password";
final md5Hash = grHMACHashString(GRCryptoTypeHMAC.hmacMD5, input, key);

Here you can see the function grHMACHashString takes two parameters,

* hashType: Specific Hash method, which should be of type *GRCryptoType*
* input: A string input to be encrypted
* key: A string key for extra security

This function will convert the input String to md5 hash as the hashType provided as GRCryptoTypeHMAC.hmacMD5

The supported values of GRCryptoTypeHMAC enum are,

* hmacSHA1,
* hmacSHA224,
* hmacSHA256,
* hmacSHA384,
* hmacSHA512,
* hmacSHA512_224,
* hmacSHA512_256,
* hmacMD5

You can find more examples of HMAC Hashing in the Example Tab

Advanced Crypto #

Advanced Hash

In the advanced section, we can encrypt one input string multiple times with different methods or with the same methods. Also,the generated hash can't be decrypted. Now, look at the given example,

const inputText = "Hello GaiNeR";
final listCryptoTypes = [GRCryptoType.sha1, GRCryptoType.md5];
final hashString = grAdvHashString(listCryptoTypes, inputText);
print(hashString);

Here you can see, we are passing two parameters to the function grAdvHashString

* hashTypes: Which is a List of *GRCryptoType*
* input: A string input to be encrypted

This function will convert the input String twice, 1st in sha1 hash and 2nd the sha1 hash to md5 hash as the hashTypes provided as GRCryptoType.sha1 and GRCryptoType.md5 sequentially in the List.

You can provide any number of GRCryptoType in the List. If a blank List is provided the output will be blank String.

* Note: Adv in grAdvHashString denotes the Advanced

Again, Look at this example,

const inputText = "Hello GaiNeR";
final sha1HashWR = grHashStringWR(GRCryptoType.sha1, 3, inputText);
print(sha1HashWR);

Here you can see, we are passing two parameters to the function grHashStringWR

* hashType: Specific Hash method, which should be of type *GRCryptoType*
* repeat: an integer, mentioning the repeat count
* input: A string input to be encrypted

1st this function will convert the input String to sha1 hash as the hashType provided as GRCryptoType.sha1.

Then output will be again encrypted in sha1 3 times as repeat count is 3.

If the repeat count is 0, then it will be encrypted only once and will not be repeated. So,

final sha1HashWR = grHashStringWR(GRCryptoType.sha1, 0, inputText);

is same as

final sha1Hash = grHashString(GRCryptoType.sha1, inputText);

If the repeat count is negative, the output will be blank String.

* Note: WR in grHashStringWR stands for With Repeat

You can find more examples of Advanced Hashing in the Example Tab

Advanced HMAC

In the advanced HMAC section, we can encrypt one input string multiple times with different methods or with the same methods. As this is the HMAC we have to use one extra key for encryption. Also,the generated hash can't be decrypted. Now, look at the given example,

const inputText = "Hello GaiNeR";
const key = "Password";
final listCryptoTypes = [GRCryptoTypeHMAC.hmacSHA1, GRCryptoTypeHMAC.hmacMD5];
final hashString = grAdvHMACHashString(listCryptoTypes, inputText, key);
print(hashString);

Here you can see, we are passing two parameters to the function grAdvHMACHashString

* hashTypes: Which is a List of *GRCryptoTypeHMAC*
* input: A string input to be encrypted
* key: A string key for extra security

This function will convert the input String twice, 1st in sha1 hash and 2nd the sha1 hash to md5 hash as the hashTypes provided as GRCryptoTypeHMAC.hmacSHA1 and GRCryptoTypeHMAC.hmacMD5 sequentially in the List.

You can provide any number of GRCryptoTypeHMAC in the List. If a blank List is provided the output will be blank String.

* Note: Adv in grAdvHashString denotes the Advanced

Again, Look at this example,

const inputText = "Hello GaiNeR";
const key = "Password";
final sha1HashWR = grHMACHashStringWR(GRCryptoTypeHMAC.hmacSHA1, 3, inputText, key);
print(sha1HashWR);

Here you can see, we are passing two parameters to the function grHMACHashStringWR

* hashType: Specific Hash method, which should be of type *GRCryptoTypeHMAC*
* repeat: an integer, mentioning the repeat count
* input: A string input to be encrypted
* key: A string key for extra security

1st this function will convert the input String to sha1 hash as the hashType provided as GRCryptoTypeHMAC.hmacSHA1.

Then output will be again encrypted in sha1 3 times as repeat count is 3.

If the repeat count is 0, then it will be encrypted only once and will not be repeated. So,

final sha1HashWR = grHMACHashStringWR(GRCryptoTypeHMAC.hmacSHA1, 0, inputText, key);

is same as

final sha1Hash = grHMACHashString(GRCryptoTypeHMAC.hmacSHA1, inputText, key);

If the repeat count is negative, the output will be blank String.

* Note: WR in grHashStringWR stands for With Repeat

You can find more examples of Advanced HMAC Hashing in the Example Tab

Key Generation #

You can generate key of type GRKey for encryption and decryption. One example,

GRKey key = grKeyFromBase16("<Base16 String>");

You can find more examples of Key Generation in the Example Tab

IV Generation #

You can generate iv of type GRIV for encryption and decryption. One example,

GRIV iv = grIVFromUtf8("My Iv");

You can find more examples of IV Generation in the Example Tab

Encryption Decryption #

AES

In this method you can encrypt or decrypt the input String with a key.

For Encryption you can use,

  1. Base16 Encryption Function
String grEncrypt16AES(
    {required String input,
    required GRKey key,
    GRIV? iv,
    GRAESMode mode = GRAESMode.sic,
    GRPadding padding = GRPadding.pkcs7,
    int repeat = 0});

This function returns a hex or base16 string

  1. Base64 Encryption Function
String grEncrypt64AES(
    {required String input,
      required GRKey key,
      GRIV? iv,
      GRAESMode mode = GRAESMode.sic,
      GRPadding padding = GRPadding.pkcs7,
      int repeat = 0});

This function returns a base64 string

For Decryption you can use,

  1. Base16 Decryption Function
dynamic grDecrypt16AES(
    {required String inputBase16,
      required GRKey key,
      GRIV? iv,
      GRAESMode mode = GRAESMode.sic,
      GRPadding padding = GRPadding.pkcs7,
      int repeat = 0});

The input should be hex or base16 String and returns a utf8 String

  1. Base64 Decryption Function
dynamic grDecrypt64AES(
    {required String inputBase64,
      required GRKey key,
      GRIV? iv,
      GRAESMode mode = GRAESMode.sic,
      GRPadding padding = GRPadding.pkcs7,
      int repeat = 0});

The input should be base64 String and returns a utf8 String

For AES, you can see in all functions only two parameters are required input and key. Rests are optional.

Supported values of GRAESMode are,

* cbc
* cfb64
* ctr
* ecb
* ofb64Gctr
* ofb64
* sic

Default is GRAESMode.sic

Supported paddings of GRPadding type are,

* pkcs7
* none

Default is GRPadding.pkcs7

You can find more examples of AES in the Example Tab

In the examples only base64 encryption and decryption are shown. Similarly you can do for Base16.

Fernet

In this method you can encrypt or decrypt the input String with a key.

For Encryption you can use,

  1. Base16 Encryption Function
String grEncrypt16Fernet({required String input, required GRKey key, GRIV? iv, int repeat = 0});

This function returns a hex or base16 string

  1. Base64 Encryption Function
String grEncrypt64Fernet({required String input, required GRKey key, GRIV? iv, int repeat = 0});

This function returns a base64 string

For Decryption you can use,

  1. Base16 Decryption Function
String grDecrypt16Fernet(
    {required String inputBase16,
    required GRKey key,
    GRIV? iv,
    int repeat = 0});

The input should be hex or base16 String and returns a utf8 String

  1. Base64 Decryption Function
String grDecrypt64Fernet(
    {required String inputBase64,
    required GRKey key,
    GRIV? iv,
    int repeat = 0});

The input should be base64 String and returns a utf8 String

For Fernet, you can see in all functions only two parameters are required input and key. Rests are optional.

You can find more examples of Fernet in the Example Tab

In the examples only base64 encryption and decryption are shown. Similarly you can do for Base16.

Salsa20

In this method you can encrypt or decrypt the input String with a key.

For Encryption you can use,

  1. Base16 Encryption Function
String grEncrypt16Salsa20({required String input, required GRKey key, GRIV? iv, int repeat = 0});

This function returns a hex or base16 string

  1. Base64 Encryption Function
String grEncrypt64Salsa20({required String input, required GRKey key, GRIV? iv, int repeat = 0});

This function returns a base64 string

For Decryption you can use,

  1. Base16 Decryption Function
String grDecrypt16Salsa20(
    {required String inputBase16,
    required GRKey key,
    GRIV? iv,
    int repeat = 0});

The input should be hex or base16 String and returns a utf8 String

  1. Base64 Decryption Function
String grDecrypt64Salsa20(
    {required String inputBase64,
    required GRKey key,
    GRIV? iv,
    int repeat = 0});

The input should be base64 String and returns a utf8 String

For Salsa20, you can see in all functions only two parameters are required input and key. Rests are optional.

You can find more example of Salsa20 in the Example Tab

In the examples only base64 encryption and decryption are shown. Similarly you can do for Base16.

RSA

In this method you can encrypt or decrypt the input String with a public key and a private key.

For Encryption you can use,

  1. Base16 Encryption Function
Future<String> grEncrypt16RSA(
    {required String input,
    required String publicKeyPath,
    required String privateKeyPath,
    GREncodingRSA encoding = GREncodingRSA.PKCS1,
    int repeat = 0});

This function returns a hex or base16 string

  1. Base64 Encryption Function
Future<String> grEncrypt64RSA(
    {required String input,
    required String publicKeyPath,
    required String privateKeyPath,
    GREncodingRSA encoding = GREncodingRSA.PKCS1,
    int repeat = 0});

This function returns a base64 string

For Decryption you can use,

  1. Base16 Decryption Function
Future<String> grDecrypt16RSA(
    {required String inputBase16,
    required String publicKeyPath,
    required String privateKeyPath,
    GREncodingRSA encoding = GREncodingRSA.PKCS1,
    int repeat = 0});

The input should be hex or base16 String and returns a utf8 String

  1. Base64 Decryption Function
Future<String> grDecrypt64RSA(
    {required String inputBase64,
    required String publicKeyPath,
    required String privateKeyPath,
    GREncodingRSA encoding = GREncodingRSA.PKCS1,
    int repeat = 0}); 

The input should be base64 String and returns a utf8 String

For RSA, you can see in all functions three parameters are required input, publicKeyPath and privateKeyPath. You have to provide path of the keys instead of direct key Strings.

Supported values for GREncodingRSA are,

* PKCS1
* OAEP

Default is GREncodingRSA.PKCS1

You can find more examples of RSA in the Example Tab

In the examples only base64 encryption and decryption are shown. Similarly you can do for Base16.

Signature Verification #

We only support SHA-256 RSA Signing and verification.

RSA Signing and Verification

For Signing you can use,

  1. Base16 Signing Function
Future<String> grSign16RSA(
    {required String input,
      required String publicKeyPath,
      required String privateKeyPath});

This function returns a hex or base16 string

  1. Base64 Signing Function
Future<String> grSign64RSA(
    {required String input,
      required String publicKeyPath,
      required String privateKeyPath});

This function returns a base64 string

For Verification you can use,

  1. Base16 Decryption Function
Future<bool> grVerify16RSA(
  {required String input, 
    required String signatureBase16,
    required String publicKeyPath,
    required String privateKeyPath});

The input should be hex or base16 String and returns a boolean value, which is true for successful verification

  1. Base64 Decryption Function
Future<bool> grVerify64RSA(
    {required String input,
      required String signatureBase64,
      required String publicKeyPath,
      required String privateKeyPath});

The input should be base64 String and returns a boolean value, which is true for successful verification

You can find more examples of Signature Verification in the Example Tab

In the examples only base64 signing and verification are shown. Similarly you can do for Base16.

Additional information #

We are enhancing this library. More features to be added in future.

7
likes
100
pub points
76%
popularity

Publisher

verified publishergnrtech.in

A Library that can produce Cryptographic hash or can encrypt and decrypt text with key.

Homepage

Documentation

API reference

License

unknown (LICENSE)

Dependencies

convert, crypto, encrypt, flutter, pointycastle

More

Packages that depend on gainer_crypto