Skip to content

Commit

Permalink
Merge pull request #1 from leocavalcante/salsa20
Browse files Browse the repository at this point in the history
Add Salsa20 Stream Cipher
  • Loading branch information
leocavalcante committed Jul 3, 2018
2 parents f09b925 + 1320db1 commit 7d940fe
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
A set of high-level APIs over PointyCastle for two-way cryptography.

- [x] AES
- [x] Salsa20
- [ ] RSA

```dart
Expand Down
1 change: 1 addition & 0 deletions lib/encrypt.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library encrypt;

export 'src/aes.dart';
export 'src/salsa20.dart';

abstract class Algorithm {
String encrypt(String plainText);
Expand Down
44 changes: 44 additions & 0 deletions lib/src/salsa20.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import '../encrypt.dart';

import 'package:pointycastle/stream/salsa20.dart';

import 'package:pointycastle/api.dart' show ParametersWithIV, KeyParameter;

import 'dart:typed_data';

import 'helpers.dart';

class Salsa20 implements Algorithm {
final String key;
final String iv;
final ParametersWithIV _params;

final Salsa20Engine _cipher = new Salsa20Engine();

Salsa20(this.key, this.iv)
: _params = new ParametersWithIV(
new KeyParameter(new Uint8List.fromList(key.codeUnits)),
new Uint8List.fromList(iv.codeUnits));

String encrypt(String plainText) {
_cipher
..reset()
..init(true, _params);

final input = new Uint8List.fromList(plainText.codeUnits);
final output = _cipher.process(input);

return formatBytesAsHexString(output);
}

String decrypt(String cipherText) {
_cipher
..reset()
..init(false, _params);

final input = createUint8ListFromHexString(cipherText);
final output = _cipher.process(input);

return new String.fromCharCodes(output);
}
}
24 changes: 20 additions & 4 deletions test/encrypt_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'package:encrypt/encrypt.dart';
import 'package:test/test.dart';

void main() {
group('AES', () {
final key = 'my32lengthsupersecretnooneknows1';
final key = 'my32lengthsupersecretnooneknows1';
final iv = '8bytesiv';

group('AES', () {
final encrypter = new Encrypter(new AES(key));
final plainText =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit ........';

final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit '
.padRight(64, '.');
final cipherText =
'db066ce180f62f020617eb720b891c1efcc48b217cb83272812a8efe3b30e7eae4373ddcede4ea77bdae77d126d95457b3759b1983bf4cb4a6a5b051a5690bdf';

Expand All @@ -17,4 +19,18 @@ void main() {
test('decrypt',
() => expect(encrypter.decrypt(cipherText), equals(plainText)));
});

group('Salsa20', () {
final encrypter = new Encrypter(new Salsa20(key, iv));

final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
final cipherText =
'5d8dae261d5a2b5960595a9480f18156bfad5f36c07c05ae59a46870d9cb1305d2502a3b35f3fc1d1977c247bee7540068efa779284161';

test('encrypt',
() => expect(encrypter.encrypt(plainText), equals(cipherText)));

test('decrypt',
() => expect(encrypter.decrypt(cipherText), equals(plainText)));
});
}

0 comments on commit 7d940fe

Please sign in to comment.