Giter Site home page Giter Site logo

xmbeat / cryppo_dart Goto Github PK

View Code? Open in Web Editor NEW

This project forked from meeco/cryppo_dart

0.0 0.0 0.0 280 KB

A simplified cryptographic library by Meeco - port of the Ruby Cryppo

License: Apache License 2.0

Ruby 1.69% Objective-C 0.05% Kotlin 0.16% Dart 97.60% Swift 0.50%

cryppo_dart's Introduction

Cryppo Dart

build and test

Cryppo Dart is a cryptographic library that enables you to encrypt and decrypt data. Cryppo Dart combines very different ciphers under one simplified API, and a set of serialization formats.

This is a Dart port of Cryppo in Ruby and JavaScript used for the Meeco platform.

Encrypt and Decrypt using a derived key

import 'package:cryppo/cryppo.dart';

Future<void> main() async {
  final encrypted = await encryptWithDerivedKey(
    data: utf8.encode('Hello World'),
    encryptionStrategy: EncryptionStrategy.aes256Gcm,
    keyDerivationStrategy: DerivationStrategy.pbkdf2Hmac,
    passphrase: 'correct horse battery staple'
  );

  print(encrypted.serialize());
  // 'Aes256Gcm.pjqdT<snip>.Pbkdf2Hmac.SzAAAA<snip>'

  // The above will include the serialized derivation artifacts but we can also get those separately:
  print(encrypted.derivationArtefacts.serialize());
  // 'Pbkdf2Hmac.SzAAAA<snip>'

  final decrypted = await decryptWithKeyDerivedFromString(
    serialized: encrypted.serialize(),
    passphrase: 'correct horse battery staple'
  );

  print(decrypted);
  // 'Hello World'
}

Encrypt and Decrypt using a generated cryptographic key

import 'package:cryppo/cryppo.dart';


Future<void> main() async {
  final key = DataEncryptionKey.generate();
  print(key.serialize());
  // fB3gwp8b...
  // can be loaded later with DataEncryptionKey.loadFromSerialized('fB3gwp8b...');

  final encrypted = await encryptWithKey(
    data: utf8.encode('Hello World'),
    key: key,
    encryptionStrategy: EncryptionStrategy.aes256Gcm,
  );

  print(encrypted.serialize());
  // 'Aes256Gcm.pjqdT....'
  // Note we are not using a derived key so the above will not include derivation artifacts

  final decrypted = await decryptWithKey({
    serialized: encrypted.serialize(),
    passphrase: key
  });

  print(utf8.decode(decrypted));
  // 'Hello World'
}

Signing and Verification

import 'package:cryppo/cryppo.dart';

Future<void> main() async {
  // Note, it is usually better to do this in an isolate for performance reasons.
  final keyPair = Rsa4096().generateKeyPair();

  // Alternatively, we can load from a PEM
  // final keyPair = KeyPair()..loadPrivateKeyFromPKCS1PemString(pemString);

  final signature = sign({
    keyPair.privateKey,
    data: utf8.encode('data to sign')
  });

  print(signature);
  // Sign.Rsa4096.hOQsys....

  // Alternatively, we could load the public key from a PEM also
  // final keyPair = KeyPair()..loadPublicKeyFromPKCS1PemString(pemString);
  final verified = verify({
    publicKey: keyPair.publicKey
    serializedSignature: signature
  });
  print(verified);
  // true
}

Encryption Strategies

Aes256Gcm

Aes256Gcm was chosen because it provides authenticated encryption. An error will be raised if an incorrect value, such as the encryption key, were used during decryption. This means you can always be sure that the decrypted data is the same as the data that was originally encrypted.

Key Derivation Strategies

Pbkdf2Hmac

Pbkdf2Hmac generates cryptographically secure keys from potentially insecure sources such as user-generated passwords.

The derived key is cryptographically secure such that brute force attacks directly on the encrypted data is infeasible. The amount of computational effort required to complete the operation can be tweaked. This ensures that brute force attacks on the password encrypted data.

cryppo_dart's People

Contributors

zbarbuto avatar nextalias avatar derek-meeco avatar v1olen avatar xmbeat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.