forked from firka/firka
Merge branch 'main' of https://git.qwit.cloud/refilc/filc-student-rewrite
This commit is contained in:
103
filc/lib/fmb_crypt.dart
Normal file
103
filc/lib/fmb_crypt.dart
Normal file
@@ -0,0 +1,103 @@
|
||||
import 'dart:typed_data';
|
||||
import 'dart:convert';
|
||||
|
||||
/// FuckMyBytes v2
|
||||
/// mnus.moe - 2025
|
||||
|
||||
/// since fmb works quite interesting, it may not throw an error on an un-decomposable string,
|
||||
/// so it is recommended to check the string to see if it is intact
|
||||
|
||||
class FMBCrypt {
|
||||
static Uint8List generateKey(String password) {
|
||||
final key = Uint8List(32);
|
||||
int hash = 0;
|
||||
|
||||
for (int i = 0; i < password.length; i++) {
|
||||
hash = ((hash << 5) - hash) + password.codeUnitAt(i);
|
||||
hash = hash & hash;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 32; i++) {
|
||||
hash = ((hash * 1664525 + 1013904223) & 0xFFFFFFFF);
|
||||
key[i] = hash & 0xFF;
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
static Uint8List generateStream(Uint8List key, int length) {
|
||||
final stream = Uint8List(length);
|
||||
final state = Uint32List(4);
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
state[i] = ((key[i * 4] << 24) |
|
||||
(key[i * 4 + 1] << 16) |
|
||||
(key[i * 4 + 2] << 8) |
|
||||
key[i * 4 + 3]) >>>
|
||||
0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
state[0] = (state[0] + state[1]) >>> 0;
|
||||
state[1] = ((state[1] << 7) | (state[1] >>> 25)) >>> 0;
|
||||
state[2] = (state[2] + state[3]) >>> 0;
|
||||
state[3] = ((state[3] << 13) | (state[3] >>> 19)) >>> 0;
|
||||
|
||||
stream[i] = (state[0] ^ state[2]) & 0xFF;
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
static Uint8List processData(dynamic data, String password) {
|
||||
try {
|
||||
final Uint8List inputData;
|
||||
if (data is Uint8List) {
|
||||
inputData = data;
|
||||
} else if (data is String) {
|
||||
inputData = Uint8List.fromList(utf8.encode(data));
|
||||
} else {
|
||||
throw ArgumentError('Data must be either String or Uint8List');
|
||||
}
|
||||
|
||||
final key = generateKey(password);
|
||||
final stream = generateStream(key, inputData.length);
|
||||
|
||||
final output = Uint8List(inputData.length);
|
||||
for (int i = 0; i < inputData.length; i++) {
|
||||
output[i] = inputData[i] ^ stream[i];
|
||||
}
|
||||
|
||||
return output;
|
||||
} catch (error) {
|
||||
throw Exception('Failed to process data: ${error.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// basic usage:
|
||||
/// String? encrypted = FMBCrypt.handleText('encrypt', 'Hello World', 'myPassword');
|
||||
/// String? decrypted = FMBCrypt.handleText('decrypt', encrypted!, 'myPassword');
|
||||
|
||||
static String? handleText(String action, String input, String password) {
|
||||
if (password.isEmpty || input.isEmpty) {
|
||||
throw ArgumentError('Password and input cannot be empty');
|
||||
}
|
||||
|
||||
try {
|
||||
if (action == 'encrypt') {
|
||||
final encrypted = processData(input, password);
|
||||
return base64Encode(encrypted);
|
||||
} else {
|
||||
try {
|
||||
final decoded = base64Decode(input);
|
||||
final decrypted = processData(decoded, password);
|
||||
return utf8.decode(decrypted);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to decrypt data: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
throw Exception('Failed to $action data: ${error.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
||||
version: 1.0.0+1
|
||||
|
||||
environment:
|
||||
sdk: ^3.6.2
|
||||
sdk: '>=3.6.0 <=3.7.0'
|
||||
|
||||
# Dependencies specify other packages that your package needs in order to work.
|
||||
# To automatically upgrade your package dependencies to the latest versions
|
||||
@@ -33,19 +33,11 @@ dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^1.0.8
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
# The "flutter_lints" package below contains a set of recommended lints to
|
||||
# encourage good coding practices. The lint set provided by the package is
|
||||
# activated in the `analysis_options.yaml` file located at the root of your
|
||||
# package. See that file for information about deactivating specific lint
|
||||
# rules and activating additional ones.
|
||||
flutter_lints: ^5.0.0
|
||||
|
||||
# For information on the generic Dart part of this file, see the
|
||||
@@ -53,10 +45,6 @@ dev_dependencies:
|
||||
|
||||
# The following section is specific to Flutter packages.
|
||||
flutter:
|
||||
|
||||
# The following line ensures that the Material Icons font is
|
||||
# included with your application, so that you can use the icons in
|
||||
# the material Icons class.
|
||||
uses-material-design: true
|
||||
|
||||
# To add assets to your application, add an assets section, like this:
|
||||
|
||||
28
filc/test/fmb_test.dart
Normal file
28
filc/test/fmb_test.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
// This is a basic Flutter widget test.
|
||||
//
|
||||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||
// utility in the flutter_test package. For example, you can send tap and scroll
|
||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||
// tree, read text, and verify that the values of widget properties are correct.
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:filc/fmb_crypt.dart';
|
||||
|
||||
void main() {
|
||||
test('encryption and decryption test', () async {
|
||||
String? encrypted = FMBCrypt.handleText('encrypt', 'Hello World!', 'test password');
|
||||
expect(encrypted, isNotNull);
|
||||
expect(encrypted, equals('eBzx7LhjOE7NW5Ba'));
|
||||
if (kDebugMode) {
|
||||
print('encrypted: $encrypted');
|
||||
}
|
||||
String? decrypted = FMBCrypt.handleText('decrypt', encrypted!, 'test password');
|
||||
expect(decrypted, isNotNull);
|
||||
expect(decrypted, equals('Hello World!'));
|
||||
if (kDebugMode) {
|
||||
print('decrypted: $decrypted');
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user