Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon1777 committed Oct 27, 2024
1 parent 802ddde commit b4beee9
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 809 deletions.
27 changes: 6 additions & 21 deletions lib/encrypt/crypter.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';

import '../utils.dart';
import 'aes/aes_cbcpkcs7.dart';
import 'kdf/kdf.dart';
import 'kdf/sha256_kdf.dart';
Expand All @@ -11,16 +11,9 @@ import 'model/keyiv.dart';
class NanoCrypt {
/// Decrypts a value with a password using AES/CBC/PKCS7
/// KDF is Sha256KDF if not specified
static Uint8List decrypt(dynamic value, String password, {KDF? kdf}) {
static Uint8List decrypt(Uint8List value, String password, {KDF? kdf}) {
kdf = kdf ?? Sha256KDF();
Uint8List valBytes;
if (value is String) {
valBytes = hexToBytes(value);
} else if (value is Uint8List) {
valBytes = value;
} else {
throw Exception('Value should be a string or a byte array');
}
Uint8List valBytes = Uint8List.fromList(value);

Uint8List salt = valBytes.sublist(8, 16);
KeyIV key = kdf.deriveKey(password, salt: salt);
Expand All @@ -35,28 +28,20 @@ class NanoCrypt {
/// KDF is Sha256KDF if not specified
static Uint8List encrypt(dynamic value, String password, {KDF? kdf}) {
kdf = kdf ?? Sha256KDF();
Uint8List valBytes;
if (value is String) {
valBytes = hexToBytes(value);
} else if (value is Uint8List) {
valBytes = value;
} else {
throw Exception('Seed should be a string or uint8list');
}
Uint8List valBytes = Uint8List.fromList(value);

// Generate a random salt
Uint8List salt = Uint8List(8);
Random rng = Random.secure();
for (int i = 0; i < 8; i++) {
salt[i] = rng.nextInt(255);
salt[i] = rng.nextInt(256);
}

KeyIV keyInfo = kdf.deriveKey(password, salt: salt);

Uint8List seedEncrypted =
AesCbcPkcs7.encrypt(valBytes, key: keyInfo.key, iv: keyInfo.iv);

return Uint8List.fromList(
stringToBytesUtf8("Salted__") + salt + seedEncrypted);
return Uint8List.fromList(utf8.encode('Salted__') + salt + seedEncrypted);
}
}
4 changes: 2 additions & 2 deletions lib/encrypt/kdf/pbkdf2_kdf.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:pointycastle/export.dart';

import '../../utils.dart';
import '../model/keyiv.dart';
import 'kdf.dart';

Expand All @@ -12,7 +12,7 @@ class PBKDF2 extends KDF {
/// Expects password to be a utf-8 string
/// If salt is not provided, a random 8-byte one will be generated
KeyIV deriveKey(String password, {Uint8List? salt}) {
Uint8List pwBytes = stringToBytesUtf8(password);
Uint8List pwBytes = utf8.encode(password);
Uint8List saltBytes = salt == null ? Uint8List(1) : salt;

// Use pbkdf2 from pointycastle
Expand Down
4 changes: 2 additions & 2 deletions lib/encrypt/kdf/sha256_kdf.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:convert';
import 'dart:typed_data';

import '../../utils.dart';
import '../model/keyiv.dart';
import '../sha.dart';
import 'kdf.dart';
Expand All @@ -12,7 +12,7 @@ import 'kdf.dart';
class Sha256KDF extends KDF {
/// Gets the key and iv
KeyIV deriveKey(String password, {Uint8List? salt}) {
Uint8List pwBytes = stringToBytesUtf8(password);
Uint8List pwBytes = utf8.encode(password);
Uint8List saltBytes = salt == null ? Uint8List(1) : salt;

// Key = sha256 (password + salt);
Expand Down
86 changes: 0 additions & 86 deletions lib/transfer/transfer_complete_sheet.dart

This file was deleted.

Loading

0 comments on commit b4beee9

Please sign in to comment.