"AES engine not initialised" with pointycastle SecureRandom() - dart

I want to generate a random BigInt with dynamik length Bits. I am using the pointycastle package to get a SecureRandom BigInt.
import 'package:pointycastle/pointycastle.dart';
void main(List<String> arguments) {
print(gen(500));
}
BigInt gen(int Bits) {
var n = BigInt.from(1);
var ran = SecureRandom('Fortuna');
n = ran.nextBigInteger(Bits);
return n;
}
This line throws an exception:
n = ran.nextBigInteger(Bits);
StateError (Bad state: AES engine not initialised)
This is the complete error in the console:
Unhandled exception:
Bad state: AES engine not initialised
#0 AESFastEngine.processBlock
package:pointycastle/block/aes_fast.dart:109
#1 BlockCtrRandom.nextUint8
package:pointycastle/random/block_ctr_random.dart:55
#2 SecureRandomBase._randomBits
package:pointycastle/…/impl/secure_random_base.dart:55
#3 SecureRandomBase.nextBigInteger
package:pointycastle/…/impl/secure_random_base.dart:33
#4 AutoSeedBlockCtrRandom.nextBigInteger.<anonymous closure>
package:pointycastle/random/auto_seed_block_ctr_random.dart:69
#5 AutoSeedBlockCtrRandom._autoReseedIfNeededAfter
package:pointycastle/random/auto_seed_block_ctr_random.dart:81
#6 AutoSeedBlockCtrRandom.nextBigInteger
package:pointycastle/random/auto_seed_block_ctr_random.dart:68
#7 FortunaRandom.nextBigInteger
package:pointycastle/random/fortuna_random.dart:46
#8 gen
bin\encrypt.dart:10
#9 main
bin\encrypt.dart:4
#10 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#11 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
I can't seem to find a solution to this error message anywhere else. I hope u guys can help me. :D

It is not clear but if I look at the examples from the project it seems like you need to call the seed method. The following works for me:
import 'dart:math';
import 'dart:typed_data';
import 'package:pointycastle/pointycastle.dart';
void main(List<String> arguments) {
print(gen(500));
}
BigInt gen(int bits) {
final _sGen = Random.secure();
var n = BigInt.from(1);
var ran = SecureRandom('Fortuna');
ran.seed(KeyParameter(
Uint8List.fromList(List.generate(32, (_) => _sGen.nextInt(255)))));
n = ran.nextBigInteger(bits);
return n;
}
The example I was inspired by: https://github.com/PointyCastle/pointycastle/blob/master/tutorials/examples/import-demo/import-demo-1.dart

Welcome to Stackoverflow.
When using any part of Pointycastle you do need to instantiate the implementation objects.
In your code you are using
var ran = SecureRandom('Fortuna');
that uses the SecureRandom class.
Simply add
final rnd = new SecureRandom("AES/CTR/PRNG");
and kindly see the PointCastle SecureRandom example for further questions:
https://github.com/PointyCastle/pointycastle/blob/master/test/random/block_ctr_random_test.dart

Related

How can I prevent FormatException: Unfinished UTF-8 octet sequence

I have downloaded a Wikipedia dump and I am trying to read it line by line. But when doing the utf8-decode I get the following error
12633: FormatException: Unfinished UTF-8 octet sequence (at offset 65536)
Stacktrace :#0 _Utf8Decoder.convertSingle (dart:convert-patch/convert_patch.dart:1789:7)
#1 Utf8Decoder.convert (dart:convert/utf.dart:351:42)
#2 Utf8Codec.decode (dart:convert/utf.dart:63:20)
#3 _MapStream._handleData (dart:async/stream_pipe.dart:213:31)
#4 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#5 _RootZone.runUnaryGuarded (dart:async/zone.dart:1618:10)
#6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#8 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
#9 _StreamController._add (dart:async/stream_controller.dart:648:7)
#10 _StreamController.add (dart:async/stream_controller.dart:596:5)
#11 _FileStream._readBlock.<anonymous closure> (dart:io/file_impl.dart:98:19)
<asynchronous suspension>
That is this line
ar جزر_غالاباغوس 1 0
So I tried saving the file utf-8 encoded with this button
But that does not seem to work
This is my code
final filePath = p.join(
Directory.current.path,
'bin\\migrate_most_views\\data\\pageviews-20220416-170000',
);
final file = File(filePath);
logger.stderr('exporting pageviews...');
StreamSubscription? reader;
int lineNumer = 0;
reader = file.openRead().map(utf8.decode).transform(LineSplitter()).listen(
(line) {
final page = MostViewedPageDaily.fromLine(line);
db.collection('page_views').insert(page.toMap());
lineNumer++;
if (lineNumer % 1000 == 0) {
logger.stdout('inserting at line $lineNumer');
}
},
onDone: () {
logger.stdout('Reader read $lineNumer lines');
reader?.cancel();
exit(0);
},
onError: (error, stackTrace) {
final message = '$lineNumer: $error\n\nStacktrace :$stackTrace';
logger.stdout(logger.ansi.error(message));
exit(1);
},
cancelOnError: true,
);
What can I do?
I downloaded the file from here
https://dumps.wikimedia.org/other/pageviews/2022/2022-04/pageviews-20220417-010000.gz
You should use file.openRead().transform(utf8.decoder) instead of file.openRead().map(utf8.decode). (Also note the argument difference: utf8.decoder is a Utf8Decoder object, and utf8.decode is a method tear-off.)
The Stream.map documentation specifically discusses this:
Unlike transform, this method does not treat the stream as chunks of a single value. Instead each event is converted independently of the previous and following events, which may not always be correct. For example, UTF-8 encoding, or decoding, will give wrong results if a surrogate pair, or a multibyte UTF-8 encoding, is split into separate events, and those events are attempted encoded or decoded independently.

clang11 Segfaulting on name mangling

I have a clang-tooling application that traverses an AST and prints it out with mangled names. It works fine in clang10, but after the (simple) port to clang11 it is segfaulting and I can't pinpoint the issue. From the backtrace, it seems that I've somehow managed to get an AST node that isn't castable.
#0 getKind () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/include/clang/AST/DeclBase.h:433
#1 classof () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/include/clang/AST/Decl.h:4242
#2 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:58
#3 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:105
#4 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:131
#5 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:121
#6 isa<clang::BlockDecl, clang::Decl const*> () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:142
#7 dyn_cast<clang::BlockDecl, clang::Decl const> () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:345
#8 getEffectiveDeclContext () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:59
#9 0x00007fb7ea08865a in mangleNameWithAbiTags () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:867
#10 0x00007fb7ea087b1b in mangleName () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:855
#11 0x00007fb7ea084374 in mangleCXXName () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:5144
...my application code...
The code that I'm running is
mangleContext_->mangleName(decl, llvm::errs());
where decl is a const NamedDecl* that corresponds to the following union:
union U { int x; int y; };
and I'm effectively trying to print the mangling of U (I'm expecting _Z1U).
I am constructing the mangle context using the following code.
ClangPrinter::ClangPrinter(clang::CompilerInstance *compiler,
clang::ASTContext *context)
: compiler_(compiler), context_(context) {
mangleContext_ = ItaniumMangleContext::create(*context, compiler->getDiagnostics());
}
Does anyone have any suggestions as to where to look to debug this issue? My guess is that I'm somehow violating an invariant of the underlying system, but since I'm only consuming the AST in a read-only manner, I don't understand how I could end up with an uncastable object.

Uncaught Error: Class 'int' has no instance getter 'length'

Am I possibly creating the stream and consequently the media instance incorrectly?
Modifying googleapis_examples/drive_upload_download_console, I'm attempting to convert already stored .docx, .xlsx, etc files to their corresponding Google Drive counterpart.
The following code
import 'dart:async';
import 'dart:io';
import 'package:http/http.dart' show Client;
import 'package:googleapis/common/common.dart' show Media, DownloadOptions;
import 'package:googleapis/drive/v2.dart' as drive;
import 'package:path/path.dart' as path;
Future convertFile(drive.DriveApi api,
Client client,
String objectId) {
var completer = new Completer();
api.files.get(objectId).then((drive.File file) {
var fileName = path.basenameWithoutExtension(file.title);
var parents = file.parents;
client.readBytes(file.downloadUrl).then((bytes) {
var driveFile = new drive.File()
..title = fileName
..mimeType = 'application/vnd.google-apps.document'
..parents = parents;
api.files.insert(driveFile)
.then((driveFile){
var byteList = bytes.toList();
var stream = new Stream.fromIterable(byteList);
var media = new Media(stream, byteList.length);
api.files.update(new drive.File(), driveFile.id, uploadMedia: media)
.then((drive.File f){
stream.close().whenComplete((){
api.files.delete(objectId)
.then((_){
completer.complete(true);
print("Converted ${f.id}");
});
});
});
});
});
});
return completer.future;
}
results in the following error.
Unhandled exception:
Uncaught Error: Class 'int' has no instance getter 'length'.
NoSuchMethodError: method not found: 'length'
Receiver: 80
Arguments: []
Stack Trace:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1 Base64Encoder.bind.onData (package:googleapis/src/common_internal.dart:325:42)
#2 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#3 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#4 _IterablePendingEvents.handleNext (dart:async/stream_impl.dart:549)
#5 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:671)
#6 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#7 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#8 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#9 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:131)
#0 _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:886)
#1 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:131)
The Media constructor expects a Stream<List<int>>, and your code is providing a Stream<int>. To solve this, just replace this line:
var stream = new Stream.fromIterable(byteList);
by:
var stream = new Stream.fromIterable([byteList]);

Dart cannot access cloud endpoint API

It's frustrating and I'm stuck here for one week and no clue how to fix it ; (
Inspired by Gerwin Sturm's great work (https://github.com/Scarygami/gdg-endpoint-demo) and I like to build the java backend instead.
But unfortunately, every time when I'm trying to call the endpoints API, a NoSuchMethodError error arise.
Exception: NoSuchMethodError : method not found: '_client#0x296594a'
Receiver: Instance of 'Dbentryendpoint'
Arguments: []
Stack Trace: #0 Object.noSuchMethod (dart:core-patch:1737:25)
#1 Dbentryendpoint.listDBEntry (http://127.0.0.1:3030/C:/Users/VincentZhou/dart/dart_dbentryendpoint_v1_api_client/packages/google_dbentryendpoint_v1_api/src/browser/dbentryendpoint.dart:162:16)
#2 main.fetch (http://127.0.0.1:3030/C:/Users/VincentZhou/dart/dart_dbentryendpoint_v1_api_client/demo.dart:24:20)
#3 main (http://127.0.0.1:3030/C:/Users/VincentZhou/dart/dart_dbentryendpoint_v1_api_client/demo.dart:64:8)
Exception: NoSuchMethodError : method not found: '_client#0x296594a'
Receiver: Instance of 'Dbentryendpoint'
Arguments: []
Stack Trace: #0 Object.noSuchMethod (dart:core-patch:1737:25)
#1 Dbentryendpoint.insertDBEntry (http://127.0.0.1:3030/C:/Users/VincentZhou/dart/dart_dbentryendpoint_v1_api_client/packages/google_dbentryendpoint_v1_api/src/browser/dbentryendpoint.dart:124:16)
#2 main.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (http://127.0.0.1:3030/C:/Users/VincentZhou/dart/dart_dbentryendpoint_v1_api_client/demo.dart:56:28)
the source code of demo.dart is almost identical to the original one (https://github.com/Scarygami/gdg-endpoint-demo) and I only change the cloudendpint API and web client ID.
import "dart:html";
import 'packages/google_dbentryendpoint_v1_api/dbentryendpoint_v1_api_browser.dart' as gdglib;
import 'packages/google_plus_v1_api/plus_v1_api_browser.dart' as pluslib;
import 'packages/google_oauth2_client/google_oauth2_browser.dart';
final CLIENT_ID = "878766780041.apps.googleusercontent.com";
final SCOPES = ["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/plus.me"];
void main() {
var auth = new GoogleOAuth2(CLIENT_ID, SCOPES);
var gdg = new gdglib.Dbentryendpoint(auth);
var plus = new pluslib.Plus(auth);
var container = query("#entries");
var loginButton = query("#login");
var sendButton = query("#send");
InputElement textInput = query("#text");
var authorSpan = query("#author");
pluslib.Person me;
void fetch() {
//gdg.makeAuthRequests = false;
gdg.makeAuthRequests = true;
gdg.listDBEntry(limit:10).then((l) {
container.text = "";
if (l.items != null) {
l.items.forEach((e) {
var p = new ParagraphElement();
var date = e.date.replaceAll("T", " ");
p.text = "$date - ${e.author}: ${e.text}";
container.append(p);
});
}
});
}
loginButton.onClick.listen((Event e) {
auth.login().then((token) {
loginButton.style.display = "none";
plus.makeAuthRequests = true;
plus.people.get("me").then((p) {
me = p;
authorSpan.text = "${me.displayName}:";
authorSpan.style.display = "inline-block";
textInput.style.display = "inline-block";
sendButton.style.display = "inline-block";
sendButton.onClick.listen((Event e) {
var text = textInput.value;
textInput.value = "";
var entry = new gdglib.DBEntry.fromJson({
"author": me.displayName,
"text": text
});
gdg.makeAuthRequests = true;
gdg.insertDBEntry(entry).then((entry) {
fetch();
});
});
});
});
});
on the Google App Engine side, I generate the cloud endpoint class automatically by the GPE and then add the client ID related info:
#Api(name = "dbentryendpoint",
clientIds={Ids.WEB_CLIENT_ID,com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
scopes={"https://www.googleapis.com/auth/userinfo.email"}
)
public class DBEntryEndpoint {
.....
Appreciate for any help!
I think I figure out the way to work around it but I don't know why:
the "generate cloud endpoints" of GPE doesn't generate the path,
for example it generate the method listEntities, not entities.list
the path is necessary to generate resource in
_ah/api/discovery/v1/apis/dbentryendpoint/v1/rest; without path ,there is only schemas and methods, not schemas and resources
without resources, the Dart client generator only export empty
resouces.dart under /commom folder. and the mothods will be inside
entityEndpoints.dart, which cannot correctly recognize the
_client(), which is defined in client.dart under /common.
So the demo.dart cannot find the method.
Question: why the path is ncessary? because GPE dont generate the path automatically, can we have a way to generate the correct dart client file without manually adding the path in google app engine file? Thanks.
The package import does not look correct, try this.
import 'packages:google_dbentryendpoint_v1_api/dbentryendpoint_v1_api_browser.dart' as gdglib;
import 'packages:google_plus_v1_api/plus_v1_api_browser.dart' as pluslib;
import 'packages:google_oauth2_client/google_oauth2_browser.dart';

Web server crashes when spammed with F5

I made a simple web server but it crashes every time I am refreshing page many times in a short time. I just enter 127.0.0.1:8080 in my browser and then spam with F5. Here is the code to reproduce this issue:
void main()
{
HttpServer server = new HttpServer();
server.addRequestHandler((req) => true, handleGET);
server.listen('127.0.0.1', 8080);
}
void handleGET(HttpRequest req, HttpResponse res)
{
var requestedFile = ".${req.path}";
if(req.path == "/")
{
requestedFile = requestedFile.concat("index.html");
}
File file = new File(requestedFile);
file.exists().then((bool found) {
if(found)
{
file.openInputStream().pipe(res.outputStream);
}
else
{
res.statusCode = HttpStatus.NOT_FOUND;
res.outputStream.close();
}
});
}
The error I get is following:
Unhandled exception:
StreamException: Stream closed
#0 _SocketOutputStream._write (dart:io:6017:30)
#1 _HttpResponse._writeHeader (dart:io:5981:18)
#2 _HttpRequestResponseBase._ensureHeadersSent (dart:io:2696:19)
#3 _HttpResponse._streamClose (dart:io:2921:23)
#4 _HttpOutputStream.close (dart:io:3078:36)
#5 _pipe.<anonymous closure> (dart:io:6271:28)
#6 _BaseDataInputStream._checkScheduleCallbacks.issueCloseCallback (dart:io:6231:59)
#7 _Timer._createTimerHandler._handleTimeout (dart:io:6804:28)
#8 _Timer._createTimerHandler._handleTimeout (dart:io:6812:7)
#9 _Timer._createTimerHandler.<anonymous closure> (dart:io:6820:23)
#10 _ReceivePortImpl._handleMessage (dart:isolate-patch:37:92)
Often before this mayor exception I receive a bunch of warnings like WSASend failed: 10053 but those don't crash the server. I work on Windows if this problem is related to some specific implementation.
Because you are hitting reload very quickly, your code ends up trying to write to a socket that has already been closed. Hence, you should probably catch StreamException and just ignore it. It could be argued that the io library should help you out a bit more. I just filed this bug:
http://code.google.com/p/dart/issues/detail?id=7334&thanks=7334&ts=1355280746

Resources