Digest value not add encrypt.Key in flutter - flutter

encrypt.key not accept digest value and it's accepting the list format value
final bytes = utf8.encode(stringKey + plainEmail);
Digest sha256Key = sha256.convert(bytes);
final key = encrypt.Key(sha256Key.bytes);
Please check below image
enter image description here

Digest.bytes returns a List<int> which, under the hood, is a Uint8List. So you need to cast it with:
final key = encrypt.Key(sha256Key.bytes as Uint8List);
since the constructor of Key expects a Uint8List.
Any time that you have a List<int> that isn't actually a Uint8List under the hood (so cannot be cast) you can convert it with:
final uInt8List = Uint8List.fromList(anyOldIntList);

Related

List<int> to string without enlarging the data file - flutter

I have a compress function like below, which has a return value List<int>
List<int> compressData() {
var stringBytes = utf8.encode(plainText);
return BZip2Encoder().encode(stringBytes);
}
then I want to encrypt the data with salsa20 from the package encrypt, but must be string data as input data, I've tried with toString() but it makes the data 2X bigger, how to solve the problem?

How to decode or convert base64 string url to UintList in Dart?

I got this base64Result from canvas.toDataURL() but I having difficulties parsing in Dart to 'UintList'
import 'dart:convert';
final String base64Result = result.toString();
print("$logTrace calling web function done ${base64Result.length}");
final bytes = base64Url.decode(base64Result);
character (at character 5)
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARwAAAIrCAYAAAA9YyZoAAAgAElEQ...
^
'data:image/png;base64,' is part of the data URL, not part of a base-64 string. You need to extract the base-64 data from the URL first.
Luckily, the UriData class can do this all for you:
final bytes = UriData.parse(base64Result).contentAsBytes();

Flutter convert image to binary data

How can i covert Image file to binary data ?
I'm using a library call image_picker for pick the image from gallery or camera.
And I want to convert the image that I picked to binary data.
File image = await ImagePicker.pickImage(source: ImageSource.gallery)
(image as Image).toByteData // the method toByteData here is not pop up.
toByteData() method allows converting the image into a byte array. We need to pass the format in the format argument which specifies the format in which the bytes will be returned. It'll return the future that completes with binary data or error.
final pngByteData = await image.toByteData(format: ImageByteFormat.png);
ImageByteFormat enum contains the following constants.
png
rawRgba
rawUnmodified
values
For more information about ImageByteFormat, please have a look at this documentation.
Update : If you want to convert the image file into bytes. Then use readAsByte() method.
var bytes = await ImagePicker.pickImage(source: ImageSource.gallery).readAsBytes();
For converting image into a file, Check out this answer.
simply use this method
var bytes = await File('filename').readAsBytes();
You might want to consider using the toByteData method.
It converts an image object into an array of bytes.
It returns a future which returns a binary image data or an error if the encoding fails.
Here is the implementation from this website: https://api.flutter.dev/flutter/dart-ui/Image/toByteData.html
Future<ByteData> toByteData({ImageByteFormat format = ImageByteFormat.rawRgba}) {
return _futurize((_Callback<ByteData> callback) {
return _toByteData(format.index, (Uint8List encoded) {
callback(encoded?.buffer?.asByteData());
});
});
}
The format argument specifies in which encoding format the bytes will be returned in.
Hope this helps!

How to get the key value from a string when we use FlutterSecureStorage?

I have the following String:
String readToken = await storage.read(key: 'token');
print(readToken);
The output will be:
flutter: "{\"accessToken\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImFiNWUwYzcwMjYwNWU1MjRmMmZkOTQ2NTAwMDQyZDk1MzBlZWZhYjhkYjA0ZGZjYj<…>
The problem is that the readToken value is a String value so I become in a Map like this.
String decoded = jsonDecode(readToken);
And the output is:
flutter: {"accessToken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImFiNWUwYzcwMjYwNWU1MjRmMmZkOTQ2NTAwMDQyZDk1MzBlZWZhYjhkYjA0ZGZjYjIwMWM1ZmE2NjJjOTQxNTA4OTg1MTZhZjBjNmIyYjRkIn0.eyJhdWQiOiIyIiwianRpIjoiYWI1ZTBjNzAyNjA1ZTUyNGYyZmQ5<…>
So it looks like is a Map value but it isn't, because is still a String.
I would like to get just the value for access_token.
I tried
String dec = decoded.replaceAll(RegExp('"'), '');
int pos = dec.indexOf(':');
final token = dec.substring(pos + 1);
You need to decode your string to transform it to an actual Map.
For that you need to use dart:convert package and do the following: json.decode(variable).
I would also suggest to store only the token value in FlutterSecureStorage and not the entire object.

FormatException Uint8List convert to string with dart

I am trying to convert a Uint8List to a string using Dart (in a Flutter project).
I am using the Flutter Android USB Serial plugin ( https://github.com/altera2015/usbserial)
The data are coming from a usb device and are returned from the library as a Stream.
If outputed as a string, it looks like:
[255,0,0,0,255....]
When I try:
String newTag = ascii.decode(asyncSnapshot.data);
I get the error :
FormatException:Invalid value in input: 255
I don't know how to solve this issue, my result should be :
"352206000079439"
Try this one;
List<int> list = 'someData'.codeUnits;
Uint8List bytes = Uint8List.fromList(list);
String string = String.fromCharCodes(bytes);
If data has compressed as a blob type.
Uint8List bytes = Uint8List.fromList(tcp_socket_blob_data);
var inflated = zlib.decode(bytes);
var data = utf8.decode(inflated);
More: flutter/dart: How to decompress/inflate zlib binary string in flutter