How to use HMAC in Lua - Lightroom plugin - lua

First thing I have to mention is I'm really really new to Lua and please be patient if you think my question is too dumb
Here is my requirement
I need to use HMAC-sha256 for Lightroom plugin development as I'm using that for security.
I was trying to use this but with no luck
https://code.google.com/p/lua-files/wiki/hmac
These are the steps I followed
Got the code of
https://code.google.com/p/lua-files/source/browse/hmac.lua and saved
as 'hmac.lua' file in my plugin directory
Got the code from this
https://code.google.com/p/lua-files/source/browse/sha2.lua and saved
as 'sha2.lua' file
Now in the file I use it like this
local hmac = require'hmac'
local sha2 = require'sha2'
--somewhere doend the line inside a function
local hashvalue = hmac.sha2('key', 'message')
but unfortunately this does not work and I'm not sure what I'm doing wrong.
Can anyone advice me what I'm doing wrong here? Or is there an easier and better way of doing this with a good example.
EDIT:
I'm doing this to get the result. When I include that code the plugin does stops working. I cannot get the output string when I do this
hashvalue = hmac.sha2('key', 'message')
local LrLogger = import 'LrLogger'
myLogger = LrLogger('FlaggedFiles')
myLogger:enable("logfile")
myLogger:trace ("=========================================\n")
myLogger:trace ('Winter is coming, ' .. hashvalue)
myLogger:trace ("=========================================\n")
and the Lightroom refuses to load the plugin and there is nothing on the log as well
Thank you very much for your help

I'd first make sure your code works outside of Lightroom. It seems that HMAC module you referenced has some other dependencies: it requires "glue", "bit", and "ffi" modules. Of these, bit and ffi are binary modules and I'm not sure you will be able to load them into Lightroom (unless they are already available there). In any case, you probably won't be able to make it run in LR if you don't have required modules and can't make it run without issues outside of LR.

If you just need to get SHA256 hash there is a way to do it Lightroom
I posted my question here and was able to get an answer. But there there was no reference of this on SDK documentation (Lightroom SDK)
local sha = import 'LrDigest'
d = sha.SHA256.digest ("Hello world")
but unfortunately there was no HMAC so I decided to use md5 with a salt because this was taking too much of my time

Spent quite some time trying to find a solution :-/
LrDigest is not documented, thanks Adobe!
Solution:
local LrDigest = import "LrDigest"
LrDigest.HMAC.digest(string, 'SHA256', key)

Related

Vite+SvelteKit - Environment variables hyper-protection

I am trying to make a POC and I'm such making a really simple use-case.
In there, I use a src/lib/db.ts who, for our interest, contains this code
console.log(import.meta.env.MONGO_URI, import.meta.env.SSR);
giving
undefined true
Of course, my .env file contains a definition for MONGO_URI, I tried with VITE_MONGO_URI and could see the value.
I know a way to expose it is to use VITE_MONGO_URI but my point is exactly not to expose it on the client-side.
I checked and the file db.ts is not bundled with the client, even the import.meta.env.SSR being true shows that the bundler knows it's happening on the server.
Question: How to access my private environment variables server-side ?
EDIT: As specified by Shriji Kondan, the API for this purpose has been created now : here
You could use dotenv on the server side, assuming you are using node-adapter, you can have a file _constants.ts in your app
import 'dotenv/config';
export const MONGO_URI = process.env.MONGO_URI;
and then import this variable into your script.
It's not very awesome to put secrets on client-side code. It should be either utilities.ts with a performed action SUPER_SECRET_API_KEY="$ecret#p1Key" in .env file, then request it via in src/lib/utilities/utility.js as explained here :
import { SUPER_SECRET_API_KEY } from '$env/static/private';
export function performApiAction() {
const apiInstance = initialiseApi({key: SUPER_SECRET_API_KEY});
}
or from page.server.ts via form actions as stated here which is preferable way but it's more complex.

TextMobject doesn't work on Jupyter-Manim

I am currently using jupyter-manim since it is the most efficient way for me to use manim. I'm running my code on Kaggle and every time I use TextMobject in manim, it outputs an error that says Latex error converting to dvi. See log output above or the log file: media/Tex/54dfbfee288272f0.log. I've tried TexMobject and Text function, but only the Text function works. The Text function is limited however, and I'm not sure how to change the font. Is there a way to fix this or is it something that comes with using jupyter-manim? It seems that all the other functions work such as drawing shapes, animating scenes, etc.
%%manim
class Text(Scene):
def construct(self):
first_line = TextMobject('Hi')
second_line = TexMobject('Hi')
#Only one that works
third_line = Text('Hi')
I tried your Manim program and it worked as expected for me. I would try making sure
include from manimlib.imports import * in your first line (importing Manim library)
include self.play(...) so you can see them
I think you already have these, but I'm putting them in case you don't.
You may also be getting the error because you do not have a LaTeX distribution installed on your system (i.e. MikTex or Texlive).
I think part of your problem may be the name of the class you chose. I had problems with your code until I changed the name from Text to TextTest. Here is a minimally working example that works fine in my Jupyter notebook (after running import jupyter_manim of course).
%%manim TextTest -p -ql
from manim import *
class TextTest(Scene):
def construct(self):
first_line = TextMobject('Hi 1')
second_line = TexMobject('Hi 2').shift(DOWN)
third_line = Text('Hi 3').shift(UP)
self.add(first_line)
self.add(second_line)
self.add(third_line)
self.wait(1)
Also, you should be aware that TextMobject and TexMobject have been deprecated.

IOS Xamarin can't read XML File

So I've search everywhere. Xamarin Docs, goggle, here, W3.
All I need to do is store some small data in an XML file.
I created the XML, got the code lined up and when i go to build it.
IOS.....Can't find file.
I've googled the answer countless times, and they all say the same thing, Make sure it is set as Content or make sure it is "Embedded Resource" I've tried it both ways, It can't find the file to access it. Is IOS really that stupid? No issues in Android, took it 30 secs. Add it to the Assets and boom there it is.
But How to get IOS to Recognize xml file(find it)?
the code is this
XDocuent doc = new XDocument.Load("StoredLogs.xml") <that line is where it throws the error, through all the break points that it is.
After this it steps through a loop to bind the data in the xml to an object
Logs a.Id = x.Element("Id).Value......
a.name......... and so
All i want is basic offline storage.
iOS really that stupid?
Yes :P
When you add the XML file as an EmbeddedResource, you need to read it from the assembly instead of the path
For example:
var readme = typeof(NameSpace.App).GetTypeInfo().Assembly
.GetManifestResourceStrean("resourcename.xml");
using (var sr = new StreamReader(readme)) {
//Read the stream
}

Get AST for C fragment, using Clang?

I am building a clang plugin, and I am trying to generate the AST for a C fragment at some point within the plugin. Something like:
std::string c_code = "...";
getAST(c_code);
Can someone point me to, how to go about this?
May be there are several ways to achieve this, but finally I got the following snippet working and to me it looks simple enough:
//arguments to the compiler
std::unique_ptr <std::vector<const char*>> args(new std::vector<const char*>());
args->push_back("my_file.c");
//do the magic
ASTUnit *au = ASTUnit::LoadFromCommandLine(
&(*args)[0],
&(*args)[0] + args->size(),
IntrusiveRefCntPtr<DiagnosticsEngine>(
CompilerInstance::createDiagnostics(new DiagnosticOptions)),
StringRef()
);
//get the translation unit node
Declr *d = au->getASTContext().getTranslationUnitDecl();
Simpler alternatives or suggestions to improve this are welcome.
I don't have a code snipped ready to copy-paste, but the idea that I used before is the following:
Note that clang_parseTranslationUnit has unsaved_files as one of the arguments. So the idea would be to provide a command line g++ main.cpp, and then provide an unsaved file with name main.cpp and the content from your string.

String encryption in RubyMotion

I'm trying to encrypt strings in RubyMotion - ideally AES but weaker/older cyphers such as Blowfish should do just fine.
So far I have failed at compiling a couple of pods: RNCrypto and CommonCrypto.
Suggestions? Anyone else tried these pods?
Thank you,
Adrian
If you're having trouble compiling CocoaPods, make sure you run a rake clean. CocoaPods should work fine with RubyMotion as far as I know.
EDIT: Since the OP hasn't posted his solution as the answer, I'll post it here:
RNCryptor doesn't build for iOS6, and there's a pull for ARC compatibility but not yet integrated in the pod.
As for CommonCrypto, it has an example.m file showcasing its capabilities. This example.m includes a main function which clashes with the one created by RubyMotion. By deleting it, I've managed to make it compile successfully.
Here's the necessary process if you currently want to use CommonCrypto pod:
make sure to include the pod in your Rakefile or perform the equivalent Bundler ritual
don't forget to include app.frameworks << 'Security' in the Rakefile as well
then go to vendor/Pods/CommonCrypto and delete the file example.m
Congratulations, you're all set!
Here's a quick (and dirty) sample:
iv = 'init_vector_here'
key = 'key_here'
plainText = 'This is plain text'
plainData = plainText.dataUsingEncoding(NSUTF8StringEncoding)
ivData = iv.dataUsingEncoding(NSUTF8StringEncoding)
keyData = key.dataUsingEncoding(NSUTF8StringEncoding)
status = NIL
result = plainData.dataEncryptedUsingAlgorithm(0, key: keyData, initializationVector: ivData, options: 0, error: status) # 0 = AES128
p result.base64EncodedString
For Base64 encoding you have to include the 'NSData+Base64' pod.
Thank you #AwDogsGo2Heaven and #Jamon Holmgren for your helpful suggestions!

Resources