Use Poco::Crypto::DigestEngine in Poco::HMACEngine - poco-libraries

Poco supports HMACEngine with different hash functions. For example to create a HMAC-SHA1 function I can use HMACEngine<SHA1Engine> hmca_sha1("secret");
This works well with DigestEngine like SHA1Engine and MD5Engine that have a constructor without argument.
Is it possible to use HMACEngine with hash functions from OpenSSL as they are provided by Poco::Crypto::DigestEngine?
The problem is that these functions take a string parameter in their constructor that specifies which hashing algorithm to use. This means HMACEngine<Poco::Crypto:DigestEngine> hmca_xxx("secret"); wouldn't work.
Any idea how to do this?

I think it will require some coding on Poco side. HMAC is accepting any class as template argument, and hoping it is a Digest class ans using methods like digest() that may not even exist. The HMACEngine that instantiates the class as template, so you can´t passa anything to its constructor as it is. In fact I don´t think that is a good use of templates, while I´m not very familiar with using this C++ resource yet, I think one should not expect anything to be available from templated class.
Probably a new HMACEngine for the Crypto module (instead of Foundation module) would be good. But looks like an approach more like the Poco::Crypto::RSADigestEngine would be better than the current template based one. I´ve recently modified Poco::Crypto::RSADigestEngine to be based on Poco::Crypto::DigestEngine instead off old Foundation Poco::DigestEngine, and therefore for next release it will also support all hashes that OpenSSL support. Basically RSADigestEngine creates an instance of a Poco::Crypto::DigestEngine as base and uses it.
I could help on a patch proposal if you create one at github. Poco::Crypto does need some updates :)

Related

Can we generate abstract Qt D-Bus adaptor classes?

I'm using the qdbusxml2cpp tool to generate a D-Bus adaptor class for my D-Bus server. However, it has the following drawbacks:
Code is generated once, and then you're not supposed to modify it. But what if we have to make changes (see below) and then the XML changes (in a backwards compatible way, of course)?
It is assumed that the "adaptee" has the exact same functions and signatures as the D-Bus interface. In my case, that's not exactly true, e.g. some methods are named differently. Because the generated code uses QMetaObject::invokeMethod, this is only detected at runtime. And we can't sensibly modify the generated code if we might need to regenerate it in the future.
It would be much nicer, in my opinion, if qdbusxml2cpp would generate an abstract class, just a header, where all methods are pure virtual. Then I can write an implementation of that class that simply calls the right methods on the adaptee, without going through the Qt metatype system. This solves both problems:
If the XML changes, we just regenerate the header. Now the compiler will complain until we implement the new interface correctly.
We have the freedom to call whatever functions we like on the "adaptee" class, instead of maintaining the exact same signature as in the public D-Bus interface.
I couldn't find any tool or qdbusxml2cpp fork that does the above. Before I write it myself, are there any problems with the above approach that I might be overlooking, design-wise or technical? Perhaps limitations of the metatype system related to abstract classes or pure virtual functions?
Note that I need this to work not just with methods, but with properties and signals as well.
I've also considered writing an "intermediate" adaptor that wraps the "adaptee" and offers the exact interface that the D-Bus adaptor expects, but the D-Bus adaptor would still be using the metatype system and runtime checks. Surely we can do better.
As you've discovered, there's no way to do what you want with qdbusxml2cpp directly. Which means that we have a few options here, some that you've already listed out:
It would be much nicer, in my opinion, if qdbusxml2cpp would generate an abstract class, just a header, where all methods are pure virtual.
There doesn't seem to be anything terribly wrong with this, although some tools/IDEs may not play nicely with it. One downside to this is that whenever something changes, you would have to ensure that you update all parts of the C++ source as well, not just your header, e.g. if there is boilerplate that has to change whenever a new method is added.
Code is generated once, and then you're not supposed to modify it. But what if we have to make changes (see below) and then the XML changes (in a backwards compatible way, of course)?
Depending on how well the code generator works, I've sometimes found it easier to simply use the generated code as a starting point and then simply modifying it from there. Most of the chages are generally pretty simple.
One other option that you could do is to use a different library to do the DBus communications.
dbus-cxx
dbus-cpp
dbus-cplusplus
I use(and maintain) dbus-cxx; there is a tool included(dbus-cxx-xml2cpp) that generates adaptee classes that are similar to the output of qdbusxml2cpp in that the adaptee class simply calls a different class that handles the actual response. The downside is that the xml2cpp tool is not that smart, and will not always output correct code. And to use dbus-cxx in a Qt application, you need to turn off the Qt keywords. However, it does have the advantage of using templated functions, so if your signature is incorrect you will get a compile error.
Unfortunately, there isn't really a good "right" way to do this, so I'm afraid that I don't have a "Do it this way" answer.

Kotlin KFunction reflection - get parameter values

In order to create a simple annotation that logs function calls, I'm trying to grab the following attributes from a function that has said annotation:
Function name
Parameter names
Parameter values
What I have so far uses KCallable as a value, which makes grabbing the name and names from the list of KParameter fairly simple. However, I cannot figure out how to get the values of said parameters to make the log statement more contextual.
Does anyone have ideas on grabbing these parameters values within the annotation? It doesn't need to use KCallable, that just seemed like the most intuitive receiver.
You will need a different approach. Annotations and parameter type are a compile time features while values are a runtime feature.
What you will have to do is use a bytecode processing framework like ASM or google "aspect oriented programming". That allows you to examine the generated bytecode and modify if before the JVM tries to execute it.
The other approach is to write a Kotlin compiler plugin which generates the necessary code (google "Writing Your First Kotlin Compiler Plugin")
This blog post contains an example for Java and Spring using the AOP approach: https://solocoding.dev/blog/eng_spring_centrlize_logging_with_aop
I recommend the compiler plugin because the other approach is much more complicated, brittle and badly documented. Use AOP only if you find a framework which already contains all the features you need.

Frama-C's extensible printer and projects

I am trying to make changes to the behavior of a function and print the results to a file. The ViewCfg plug-in described in the Plug-in Development Guide does something similar, but I am trying to avoid having to use Ast.get, which ViewCfg uses. I am thinking of extending Printer.extensible_printer which, according to the Frama-C API Documentation, is something I can do if I want to obtain a custom pretty-printer.
However, if I extend the pretty-printer as described in the API docs, unless I'm doing something wrong, I notice that the changes I make take place regardless of which project is set as the current project. I'm using File.create_project_from_visitor to create a new project and Project.set_current to set the new project as the current project before I use the custom pretty-printer.
Is it true that any change made by a class that extends Printer.extensible_printer applies to all projects? I happen to be using frama-c-Aluminium-20160502, which I know is not the latest version.
EDIT: Sorry, I should have made this clearer in the beginning, but I'm not actually making changes to the behavior of a function. I'm reading in the behavior of a function, then based on that, I'm trying to generate as output valid C code that's meant to be read as input by another program.
Regarding AST.get, the only reason I was avoiding it was that it sounds like it gets the entire AST, while I'm only interested in part of it, i.e. behaviors. But if I'm just making things harder for myself by avoiding it, then I'll go ahead and use it.

How to create a closure from String in Dart?

How to use dart-mirror API to create a anonymous closure dynamically?
Like as the interpreter, compile the code during run-time.
var funcstr='bool (String s){ return (s==null); }';
var func=parseStr(funcstr);
// func(s)-> s==null;
var r=func('false');
// r=false;
so, how to do with "parseStr"?
my project:
http://github.com/stevehsu77/surebet
At the moment there is no way to do this. Dart has no eval and no code generation at runtime.
But it is something Gilad Bracha (the language spec lead of Dart) wants to have (https://groups.google.com/a/dartlang.org/forum/#!topic/misc/6O4g7eEHgOU) at least for the development environment.
Also
We’d like to support more powerful reflective features in the future. These would include mirror builders, designed to allow programs to extend and modify themselves, and a mirror-based debugging API as well.
https://www.dartlang.org/articles/reflection-with-mirrors/
So it'll probably be supported some time in the future. But right now it's not possible.
As mentioned above, Dart does not have eval, however it is possible to load new source code in another isolate using spawnUri().
I am not sure if there are any examples of how to use this. Perhaps post a message on the dart discussion group.
Using isolates and spawnUri() is quite a different than using eval, so it may not be the right fit for your project.

How to call calabash xml from a Java program

I try to set up a web based application using spring and xslt. Since i always use xslt in a pipelining style, i would like to use calabash. Is there a possibility to call calabash from Java? I read thru the documentation on http://xmlcalabash.com but there is only a description how to use it from command line. I also tired to find some javadoc on githup but wasn't successful. Obviously, there is the Main class with the main() method and i could supply the command line parameters as a string array...
I wonder if there is a better way to do it.
I looked into this recently too. I took a pragmatic approach where I call Main.run(), and pass in a string array that I generate from a (File)Properties object. It doesn't allow passing in file inputs as streams or sources however, they must reside on the file-system.
Likely there are nicer ways. You could for instance look into http://expath.org/ . There should be sources of that project. The webapp modules (formerly known as servlex?) seems to provide XMLCalabash integration.
HTH!

Resources