• Stars
    star
    323
  • Rank 130,051 (Top 3 %)
  • Language
    Dart
  • License
    BSD 3-Clause "New...
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔒 A set of high-level APIs over PointyCastle for two-way cryptography.

encrypt

Pub Package Build Status Donate

A set of high-level APIs over PointyCastle for two-way cryptography.

Looking for password hashing? Please, visit password.

Secure random

You can generate cryptographically secure random keys and IVs for you project.

Activate the encrypt package:

pub global activate encrypt

Then use the secure-random command-line tool:

$ secure-random
CBoaDQIQAgceGg8dFAkMDBEOECEZCxgMBiAUFQwKFhg=

You can set the length and the base output.

$ secure-random --help
-l, --length       The length of the bytes
                   (defaults to "32")

-b, --base         Bytes represented as base 64 or base 16 (Hexdecimal)
                   (defaults to "64")

-h, --[no-]help    Show this help message

Algorithms

Current status is:

  • AES with PKCS7 padding
  • RSA with PKCS1 and OAEP encoding
  • Salsa20

Signing

  • SHA256 with RSA

Usage

Symmetric

AES

import 'package:encrypt/encrypt.dart';

void main() {
  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final key = Key.fromUtf8('my 32 length key................');
  final iv = IV.fromLength(16);

  final encrypter = Encrypter(AES(key));

  final encrypted = encrypter.encrypt(plainText, iv: iv);
  final decrypted = encrypter.decrypt(encrypted, iv: iv);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // R4PxiU3h8YoIRqVowBXm36ZcCeNeZ4s1OvVBTfFlZRdmohQqOpPQqD1YecJeZMAop/hZ4OxqgC1WtwvX/hP9mw==
}
Modes of operation

Default mode is SIC AESMode.sic, you can override it using the mode named parameter:

final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
Supported modes are:
  • CBC AESMode.cbc
  • CFB-64 AESMode.cfb64
  • CTR AESMode.ctr
  • ECB AESMode.ecb
  • OFB-64/GCTR AESMode.ofb64Gctr
  • OFB-64 AESMode.ofb64
  • SIC AESMode.sic
No/zero padding

To remove padding, pass null to the padding named parameter on the constructor:

final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: null));

Salsa20

import 'package:encrypt/encrypt.dart';

void main() {
  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final key = Key.fromLength(32);
  final iv = IV.fromLength(8);
  final encrypter = Encrypter(Salsa20(key));

  final encrypted = encrypter.encrypt(plainText, iv: iv);
  final decrypted = encrypter.decrypt(encrypted, iv: iv);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // CR+IAWBEx3sA/dLkkFM/orYr9KftrGa7lIFSAAmVPbKIOLDOzGwEi9ohstDBqDLIaXMEeulwXQ==
}

Fernet

import 'package:encrypt/encrypt.dart';
import 'dart:convert';

void main() {
  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final key = Key.fromUtf8('my32lengthsupersecretnooneknows1');

  final b64key = Key.fromUtf8(base64Url.encode(key.bytes).substring(0,32));
  // if you need to use the ttl feature, you'll need to use APIs in the algorithm itself
  final fernet = Fernet(b64key);
  final encrypter = Encrypter(fernet);

  final encrypted = encrypter.encrypt(plainText);
  final decrypted = encrypter.decrypt(encrypted);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // random cipher text
  print(fernet.extractTimestamp(encrypted.bytes)); // unix timestamp
}

Asymmetric

RSA

import 'dart:io';
import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/asymmetric/api.dart';

void main() {
  final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
  final privKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');

  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final encrypter = Encrypter(RSA(publicKey: publicKey, privateKey: privKey));

  final encrypted = encrypter.encrypt(plainText);
  final decrypted = encrypter.decrypt(encrypted);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // kO9EbgbrSwiq0EYz0aBdljHSC/rci2854Qa+nugbhKjidlezNplsEqOxR+pr1RtICZGAtv0YGevJBaRaHS17eHuj7GXo1CM3PR6pjGxrorcwR5Q7/bVEePESsimMbhHWF+AkDIX4v0CwKx9lgaTBgC8/yJKiLmQkyDCj64J3JSE=
}

Signature and verification

RSA

 final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
 final privateKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');
 final signer = Signer(RSASigner(RSASignDigest.SHA256, publicKey: publicKey, privateKey: privateKey));

 print(signer.sign('hello world').base64);
 print(signer.verify64('hello world', 'jfMhNM2v6hauQr6w3ji0xNOxGInHbeIH3DHlpf2W3vmSMyAuwGHG0KLcunggG4XtZrZPAib7oHaKEAdkHaSIGXAtEqaAvocq138oJ7BEznA4KVYuMcW9c8bRy5E4tUpikTpoO+okHdHr5YLc9y908CAQBVsfhbt0W9NClvDWegs='));

More Repositories

1

siler

⚡ Flat-files and plain-old PHP functions rockin'on as a set of general purpose high-level abstractions.
PHP
1,127
star
2

page_view_indicator

👆🏻 Builds indication marks for PageView.
Dart
160
star
3

swoole-futures

⏳ Futures, Streams & Async/Await for PHP's Swoole asynchronous run-time.
PHP
105
star
4

Docktober

🍂 Simple: Docker + OctoberCMS
56
star
5

hyperf-doctrine

🎲 This project provides an integration for the Doctrine ORM and the Hyperf framework.
PHP
52
star
6

password-dart

A set of high-level APIs over PointyCastle and CryptoUtils to hash and verify passwords securely.
Dart
49
star
7

ReduRx

👌 A thin layer of a Redux-based state manager on top of RxDart
Dart
41
star
8

observable_state

🔭 Flutter's State Manager for Reactive Apps in a Centralized and Predictable container.
Dart
39
star
9

aws-lambda-swoole-runtime

λ Run PHP Coroutines & Fibers as-a-Service on the AWS Lambda.
PHP
38
star
10

awesome-payments-br

Awesome list of payment gateways in Brazil
38
star
11

dwoole

⚙️ Docker image for Swoole apps with Composer, auto-restart on development and a production-ready version.
PHP
32
star
12

my-awesome-frontend

🎈 Just one link per motivation
30
star
13

up-n-running-k8s

💠 Step-by-step guide of a custom and universal setup for plain VPSs or bare-metal servers using free and open tools.
30
star
14

ippo

Immutable, Statically-typed, Cloneable and Serializable Auto-generated Plain-old PHP Objects
PHP
29
star
15

Flutter-ReduRx

🎯 Flutter bindings for ReduRx.
Dart
25
star
16

meteor-redux

Meteorux? Reduxor? Let's find out!
JavaScript
24
star
17

request-callback

➰ Swoole request callback for PSR compliant handlers.
PHP
24
star
18

redact-sensitive

🙈 A Monolog processor that protects sensitive data from miss logging
PHP
22
star
19

swoole-postgresql-doctrine-driver

🔌 A Doctrine DBAL Driver implementation on top of Swoole Coroutine PostgreSQL client
PHP
22
star
20

kafka-phplayground

🎸 Just some fun with Kafka, PHP and Swoole
PHP
20
star
21

inmana-php

🚀 Developing Rocketseat's Next Level Week (NLW#05) Application using PHP/Swoole + Hyperf
PHP
20
star
22

create-siler-app

🧱 Set up a modern Siler app by running one command.
PHP
16
star
23

intel-codenames-picker

🤔 Pick a random Intel codename for you next project!
JavaScript
16
star
24

oc-websockets

Add real-time features to your OctoberCMS project.
PHP
15
star
25

shape

Run-time type checks against plain old PHP arrays
PHP
14
star
26

rustancean-radar

🦀 A fearless approach to Rocketseat's OminiStack week.
Rust
14
star
27

swoole-irc-client

💬 Swoole based IRC (Internet Relay Chat) Client
PHP
14
star
28

Fusic

🎵 Music with F#: The Language and the Note
F#
13
star
29

rphc

PHP-to-PHP RPC Framework
PHP
12
star
30

nginxless

PoC of Swoole serving PHP-FPM requests for popular frameworks like Laravel & WordPress.
PHP
9
star
31

The-Minimal-React

Before create-react-app was cool
JavaScript
9
star
32

be-the-hero

OminiStack week v11.0... but in Rust! 🦀
Rust
8
star
33

php-go-scylladb

👻 Proof of concept on making CQL queries to ScyllaDB from PHP using Spiral's Goridge and Go's shard-aware driver.
PHP
8
star
34

publican

🧔 The package that will help you out with Dart's Pub 🎯
Dart
8
star
35

php-algebraic-effects

🥓 PoC of Algebraic Effects in PHP programming language
PHP
7
star
36

nanoroku

⚛️ Hyperf's Nano + Salesforce's Heroku
PHP
7
star
37

swoole-mutex

🚦 Mutual exclusion abstractions for PHP's Swoole concurrency run-time.
PHP
7
star
38

closure-chain

⛓️ Chain of responsibility pattern for your Closures
PHP
7
star
39

result

🎁 Result provides an alternative error handling mechanism, avoiding throwing exceptions or using nulls.
PHP
7
star
40

swoole-graphql-api

Building a GraphQL API on top of Swoole.
PHP
7
star
41

newrelic-telemetry-sdk-php

Unofficial PHP library for sending telemetry data to New Relic.
PHP
7
star
42

rust-snake

🐍 Learning Rust doing a Snake game with the help of SDL2.
Rust
6
star
43

oc-minimal-react

Get started with React on OctoberCMS.
JavaScript
6
star
44

tracing-nano

🔭 Proof of concept on adding observability features (tracing and metrics) to a Nano microservice (using existing Hyperf components).
PHP
5
star
45

vlog

✌️ Building a 150 KB web blog in V & SQLite
V
5
star
46

viewi-swoole

🚀 Serving a Viewi application with a Swoole server.
PHP
5
star
47

oc-backendusertree

Adds SimpleTree trait functionality to OctoberCMS's backend users.
PHP
5
star
48

cheer-your-title

Sometimes your <title> tag needs some joy
JavaScript
4
star
49

fsharp-mongodb-api

REST API with MongoDB and F# on .NET Core.
F#
4
star
50

crisgon

Kind-of compiler fun to generate CSS pixel images
JavaScript
3
star
51

swoole-mysql-lock

🔒 Experimenting MySQL locks for concurrent Swoole processes.
PHP
3
star
52

phull

Client-server communication between JS and PHP via Ajax long polling.
PHP
3
star
53

mars

Meteor, Apollo, Redux & React
JavaScript
3
star
54

simplest-php-website-ever

Simplest PHP website ever!
PHP
3
star
55

coroutine-context-api

Using Coroutines Contexts as Hierarchal Service Locators and Dependency Injection Containers
PHP
3
star
56

loadtest

HTTP server snippets to load test.
Batchfile
3
star
57

dotfiles

My personal dotfiles
Emacs Lisp
2
star
58

free-deploy

💸 Full-stack React (Next.js) & MongoDB application exploring generous free plans from Vercel and Atlas.
TypeScript
2
star
59

dispatch

🕊️ Event dispatcher awareness made simple.
PHP
2
star
60

merw6n

MEAN? MERW6N! WTF?
JavaScript
2
star
61

otel-php-metrics

Very small example application to debug HTTP requests no summing up.
PHP
2
star
62

yet-another-clean-architecture-example

📐 YACAE shows off a proof of concept for a Clean Architecture trying to follow some basic principles
PHP
2
star
63

exercism-rust

🏋️ My solutions for Exercism's Rust track
Rust
1
star
64

nano-goridge

Proof of concept on running Hyperf's Nano with Spiral's Goridge
PHP
1
star
65

rusty-rocket

Working out on Rust and Rocket web framework
Rust
1
star
66

dartlangbr

Exemplo de como criar e publicar pacotes de bibliotecas Dart no Pub.
Dart
1
star
67

swoole-examples

📓 Learning and teaching Swoole by examples
PHP
1
star
68

php-dev

🐘 Docker based development environment for PHP source-code.
Dockerfile
1
star
69

go-intensivo

Working on Go Intensivo class
Go
1
star
70

phpaskell

Trying to implement Category Theory concepts in PHP to help understanding Haskell
PHP
1
star
71

write

Notes, ideas, articles, screenwriting, papers, drafts, thoughts... Just write!
1
star
72

phusic

PHP port of the Fusic experiment
PHP
1
star
73

risp

🦀📜 (rust-based (cli-tool (to-work-with (lists))))
Rust
1
star
74

hello-dapr

🎩 Running PHP's Dapr SDK within Swoole & Hyperf.
PHP
1
star
75

leocavalcante.org

*.org
Makefile
1
star
76

slimr

Wire up dependencies, routes and hooks in your Slim application and use Controllers as Services.
PHP
1
star
77

tracking-analytics

Track any kind of action in a Google Analytics account even out of a page you control (i.e. Pixel Tracking)
PHP
1
star
78

nedo

󠀼Nedo.html is a HTML runtime built as a PoC for people who think markup languages can't be programming languages.
Rust
1
star