-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from leocavalcante/salsa20
Add Salsa20 Stream Cipher
- Loading branch information
Showing
4 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters