Skip to content

Commit

Permalink
uses nullable parameters wherever needed
Browse files Browse the repository at this point in the history
replaces @required attribute with required keyword
replaces utf8.encode with utf8.encoder.convert to avoid Uint8List casts
  • Loading branch information
kozw committed Jan 4, 2021
1 parent d228b7a commit 3dd6795
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 124 deletions.
40 changes: 22 additions & 18 deletions lib/src/cha_cha20_poly1305.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,66 @@ class ChaCha20Poly1305 {

/// Encrypts a message with optional additional data, a key and a nonce.
static Uint8List encrypt(Uint8List value, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305Encrypt(
value, additionalData, null, nonce, key);

/// Verifies and decrypts a cipher text produced by encrypt.
static Uint8List decrypt(Uint8List cipherText, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305Decrypt(
null, cipherText, additionalData, nonce, key);

/// Encrypts a string message with optional additional data, a key and a nonce.
static Uint8List encryptString(String value, Uint8List nonce, Uint8List key,
{String additionalData}) =>
encrypt(utf8.encode(value), nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
{String? additionalData}) =>
encrypt(utf8.encoder.convert(value), nonce, key,
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);

/// Verifies and decrypts a cipher text produced by encrypt.
static String decryptString(
Uint8List cipherText, Uint8List nonce, Uint8List key,
{String additionalData}) {
{String? additionalData}) {
final m = decrypt(cipherText, nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);
return utf8.decode(m);
}

/// Encrypts a message with optional additional data, a key and a nonce. Returns a detached cipher text and mac.
static DetachedCipher encryptDetached(
Uint8List value, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305EncryptDetached(
value, additionalData, null, nonce, key);

/// Verifies and decrypts a cipher text and mac produced by encrypt detached.
static Uint8List decryptDetached(
Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305DecryptDetached(
null, cipher, mac, additionalData, nonce, key);

/// Encrypts a string message with optional additional data, a key and a nonce. Returns a detached cipher text and mac.
static DetachedCipher encryptStringDetached(
String value, Uint8List nonce, Uint8List key,
{String additionalData}) =>
encryptDetached(utf8.encode(value), nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
{String? additionalData}) =>
encryptDetached(utf8.encoder.convert(value), nonce, key,
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);

/// Verifies and decrypts a cipher text and mac produced by encrypt detached.
static String decryptStringDetached(
Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key,
{String additionalData}) {
{String? additionalData}) {
final m = decryptDetached(cipher, mac, nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);
return utf8.decode(m);
}
}
40 changes: 22 additions & 18 deletions lib/src/cha_cha20_poly1305_ietf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,66 @@ class ChaCha20Poly1305Ietf {

/// Encrypts a message with optional additional data, a key and a nonce.
static Uint8List encrypt(Uint8List value, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305IetfEncrypt(
value, additionalData, null, nonce, key);

/// Verifies and decrypts a cipher text produced by encrypt.
static Uint8List decrypt(Uint8List cipherText, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305IetfDecrypt(
null, cipherText, additionalData, nonce, key);

/// Encrypts a string message with optional additional data, a key and a nonce.
static Uint8List encryptString(String value, Uint8List nonce, Uint8List key,
{String additionalData}) =>
encrypt(utf8.encode(value), nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
{String? additionalData}) =>
encrypt(utf8.encoder.convert(value), nonce, key,
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);

/// Verifies and decrypts a cipher text produced by encrypt.
static String decryptString(
Uint8List cipherText, Uint8List nonce, Uint8List key,
{String additionalData}) {
{String? additionalData}) {
final m = decrypt(cipherText, nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);
return utf8.decode(m);
}

/// Encrypts a message with optional additional data, a key and a nonce. Returns a detached cipher text and mac.
static DetachedCipher encryptDetached(
Uint8List value, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305IetfEncryptDetached(
value, additionalData, null, nonce, key);

/// Verifies and decrypts a cipher text and mac produced by encrypt detached.
static Uint8List decryptDetached(
Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key,
{Uint8List additionalData}) =>
{Uint8List? additionalData}) =>
Sodium.cryptoAeadChacha20poly1305IetfDecryptDetached(
null, cipher, mac, additionalData, nonce, key);

/// Encrypts a string message with optional additional data, a key and a nonce. Returns a detached cipher text and mac.
static DetachedCipher encryptStringDetached(
String value, Uint8List nonce, Uint8List key,
{String additionalData}) =>
encryptDetached(utf8.encode(value), nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
{String? additionalData}) =>
encryptDetached(utf8.encoder.convert(value), nonce, key,
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);

/// Verifies and decrypts a cipher text and mac produced by encrypt detached.
static String decryptStringDetached(
Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key,
{String additionalData}) {
{String? additionalData}) {
final m = decryptDetached(cipher, mac, nonce, key,
additionalData:
additionalData != null ? utf8.encode(additionalData) : null);
additionalData: additionalData != null
? utf8.encoder.convert(additionalData)
: null);
return utf8.decode(m);
}
}
4 changes: 2 additions & 2 deletions lib/src/crypto_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class CryptoAuth {

/// Computes a tag for given string value and key.
static Uint8List computeString(String value, Uint8List key) =>
compute(utf8.encode(value), key);
compute(utf8.encoder.convert(value), key);

/// Verifies that the tag is valid for given string value and key.
static bool verifyString(Uint8List tag, String value, Uint8List key) =>
verify(tag, utf8.encode(value), key);
verify(tag, utf8.encoder.convert(value), key);
}
8 changes: 4 additions & 4 deletions lib/src/crypto_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CryptoBox {
/// Encrypts a string message with a key and a nonce.
static Uint8List encryptString(String value, Uint8List nonce,
Uint8List publicKey, Uint8List secretKey) =>
encrypt(utf8.encode(value), nonce, publicKey, secretKey);
encrypt(utf8.encoder.convert(value), nonce, publicKey, secretKey);

/// Verifies and decrypts a cipher text produced by encrypt.
static String decryptString(Uint8List cipherText, Uint8List nonce,
Expand All @@ -59,7 +59,7 @@ class CryptoBox {
/// Encrypts a string message with a key and a nonce, returning the encrypted message and authentication tag
static DetachedCipher encryptStringDetached(String value, Uint8List nonce,
Uint8List publicKey, Uint8List secretKey) =>
encryptDetached(utf8.encode(value), nonce, publicKey, secretKey);
encryptDetached(utf8.encoder.convert(value), nonce, publicKey, secretKey);

/// Verifies and decrypts a detached cipher text and tag.
static String decryptStringDetached(Uint8List cipher, Uint8List mac,
Expand All @@ -81,7 +81,7 @@ class CryptoBox {
/// Encrypts a string message with a key and a nonce.
static Uint8List encryptStringAfternm(
String value, Uint8List nonce, Uint8List k) =>
encryptAfternm(utf8.encode(value), nonce, k);
encryptAfternm(utf8.encoder.convert(value), nonce, k);

/// Verifies and decrypts a cipher text produced by encrypt.
static String decryptStringAfternm(
Expand All @@ -103,7 +103,7 @@ class CryptoBox {
/// Encrypts a string message with a key and a nonce, returning the encrypted message and authentication tag
static DetachedCipher encryptStringDetachedAfternm(
String value, Uint8List nonce, Uint8List k) =>
encryptDetachedAfternm(utf8.encode(value), nonce, k);
encryptDetachedAfternm(utf8.encoder.convert(value), nonce, k);

/// Verifies and decrypts a detached cipher text and tag.
static String decryptStringDetachedAfternm(
Expand Down
10 changes: 5 additions & 5 deletions lib/src/crypto_sign.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CryptoSign {

/// Prepends a signature to specified string message for given secret key.
static Uint8List signString(String message, Uint8List secretKey) =>
sign(utf8.encode(message), secretKey);
sign(utf8.encoder.convert(message), secretKey);

/// Checks the signed message using given public key and returns the message with the signature removed.
static Uint8List open(Uint8List signedMessage, Uint8List publicKey) =>
Expand All @@ -41,7 +41,7 @@ class CryptoSign {

/// Computes a signature for given string message and secret key.
static Uint8List signStringDetached(String message, Uint8List secretKey) =>
signDetached(utf8.encode(message), secretKey);
signDetached(utf8.encoder.convert(message), secretKey);

/// Verifies whether the signature is valid for given message using the signer's public key.
static bool verify(
Expand All @@ -51,7 +51,7 @@ class CryptoSign {
/// Verifies whether the signature is valid for given string message using the signer's public key.
static bool verifyString(
Uint8List signature, String message, Uint8List publicKey) =>
verify(signature, utf8.encode(message), publicKey);
verify(signature, utf8.encoder.convert(message), publicKey);

/// Computes a signature for given stream and secret key.
static Future<Uint8List> signStream(
Expand Down Expand Up @@ -87,7 +87,7 @@ class CryptoSign {
final state = Sodium.cryptoSignInit();
try {
await for (var value in stream) {
Sodium.cryptoSignUpdate(state, utf8.encode(value));
Sodium.cryptoSignUpdate(state, utf8.encoder.convert(value));
}
return Sodium.cryptoSignFinalCreate(state, secretKey);
} finally {
Expand All @@ -101,7 +101,7 @@ class CryptoSign {
final state = Sodium.cryptoSignInit();
try {
await for (var value in stream) {
Sodium.cryptoSignUpdate(state, utf8.encode(value));
Sodium.cryptoSignUpdate(state, utf8.encoder.convert(value));
}
return Sodium.cryptoSignFinalVerify(state, signature, publicKey) == 0;
} finally {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/crypto_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ class CryptoStream {

/// Encrypts specified string value using a nonce and a secret key.
static Uint8List xorString(String value, Uint8List nonce, Uint8List key) =>
xor(utf8.encode(value), nonce, key);
xor(utf8.encoder.convert(value), nonce, key);
}
3 changes: 1 addition & 2 deletions lib/src/detached_cipher.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'dart:typed_data';
import 'package:flutter/foundation.dart';

/// Detached cipher and associated authentication tag.
class DetachedCipher {
final Uint8List c, mac;

const DetachedCipher({@required this.c, @required this.mac});
const DetachedCipher({required this.c, required this.mac});
}
12 changes: 6 additions & 6 deletions lib/src/generic_hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ class GenericHash {
static Uint8List randomKey() => Sodium.cryptoGenerichashKeygen();

/// Computes a generic hash of specified length for given value and optional key.
static Uint8List hash(Uint8List value, {Uint8List key, int outlen}) {
static Uint8List hash(Uint8List value, {Uint8List? key, int? outlen}) {
outlen ??= Sodium.cryptoGenerichashBytes;
return Sodium.cryptoGenerichash(outlen, value, key);
}

/// Computes a generic hash of specified length for given string value and optional key.
static Uint8List hashString(String value, {Uint8List key, int outlen}) =>
hash(utf8.encode(value), key: key, outlen: outlen);
static Uint8List hashString(String value, {Uint8List? key, int? outlen}) =>
hash(utf8.encoder.convert(value), key: key, outlen: outlen);

/// Computes a generic hash of specified length for given stream of byte values and optional key.
static Future<Uint8List> hashStream(Stream<Uint8List> stream,
{Uint8List key, int outlen}) async {
{Uint8List? key, int? outlen}) async {
outlen ??= Sodium.cryptoGenerichashBytes;
final state = Sodium.cryptoGenerichashInit(key, outlen);
try {
Expand All @@ -35,12 +35,12 @@ class GenericHash {

/// Computes a generic hash of specified length for given stream of string values and optional key.
static Future<Uint8List> hashStrings(Stream<String> stream,
{Uint8List key, int outlen}) async {
{Uint8List? key, int? outlen}) async {
outlen ??= Sodium.cryptoGenerichashBytes;
final state = Sodium.cryptoGenerichashInit(key, outlen);
try {
await for (var value in stream) {
Sodium.cryptoGenerichashUpdate(state, utf8.encode(value));
Sodium.cryptoGenerichashUpdate(state, utf8.encoder.convert(value));
}
return Sodium.cryptoGenerichashFinal(state, outlen);
} finally {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class Hash {
static Uint8List hash(Uint8List value) => Sodium.cryptoHash(value);

/// Computes a hash for given string value.
static Uint8List hashString(String value) => hash(utf8.encode(value));
static Uint8List hashString(String value) =>
hash(utf8.encoder.convert(value));
}
3 changes: 1 addition & 2 deletions lib/src/init_push_result.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:ffi';
import 'dart:typed_data';
import 'package:flutter/foundation.dart';

class InitPushResult {
final Pointer<Uint8> state;
final Uint8List header;

const InitPushResult({@required this.state, @required this.header});
const InitPushResult({required this.state, required this.header});
}
4 changes: 2 additions & 2 deletions lib/src/key_derivation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class KeyDerivation {

/// Derives a subkey from given master key.
static Uint8List derive(Uint8List masterKey, int subKeyId,
{int subKeyLength, String context = '00000000'}) {
{int? subKeyLength, String context = '00000000'}) {
subKeyLength ??= Sodium.cryptoKdfBytesMin;
return Sodium.cryptoKdfDeriveFromKey(
subKeyLength, subKeyId, utf8.encode(context), masterKey);
subKeyLength, subKeyId, utf8.encoder.convert(context), masterKey);
}
}
3 changes: 1 addition & 2 deletions lib/src/key_pair.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'dart:typed_data';
import 'package:flutter/foundation.dart';

/// Represents a secret key and a corresponding public key.
class KeyPair {
final Uint8List pk, sk;

const KeyPair({@required this.pk, @required this.sk});
const KeyPair({required this.pk, required this.sk});
}
6 changes: 3 additions & 3 deletions lib/src/onetime_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ class OnetimeAuth {

/// Computes a tag for given string value and key.
static Uint8List computeString(String value, Uint8List key) =>
compute(utf8.encode(value), key);
compute(utf8.encoder.convert(value), key);

/// Verifies that the tag is valid for given string value and key.
static bool verifyString(Uint8List tag, String value, Uint8List key) =>
verify(tag, utf8.encode(value), key);
verify(tag, utf8.encoder.convert(value), key);

// Computes a tag for given stream of strings and key.
static Future<Uint8List> computeStrings(
Stream<String> stream, Uint8List key) async {
final state = Sodium.cryptoOnetimeauthInit(key);
try {
await for (var value in stream) {
Sodium.cryptoOnetimeauthUpdate(state, utf8.encode(value));
Sodium.cryptoOnetimeauthUpdate(state, utf8.encoder.convert(value));
}
return Sodium.cryptoOnetimeauthFinal(state);
} finally {
Expand Down
Loading

0 comments on commit 3dd6795

Please sign in to comment.