Page 1 of 1

[Solved] Compute MD5 checksum of a string with AOO Basic

Posted: Fri Jul 22, 2016 10:49 pm
by yiorisos
Hallo! I'm trying to create a code, that translates wiki source code to OpenOffice writer (see https://de.wikibooks.org/wiki/Benutzer: ... amm_selber). For Wikimedia sources i need the md5 checksum. I already found out that there is a MD5Thumbprint in the Documentation, so there must be a way to generate the md5 checksum for any string. Is there anybody that can help me on this? To be more exact: I need a command or a combination like stringMD5=s.md5(), where s will be a string (for example something like "Douglas C-47 Skytrain.jpg") and stringMD5 should be a string or something that I can make to a string (in this example the outcome in the end should be the string "40decfb5f1be8bca1e56c8a853027941"). thanx in advance! Georg

Re: how do I compute the MD5 checksum of a string?

Posted: Fri Jul 22, 2016 11:13 pm
by Villeroy
Your Ubuntu comes with a program called "md5sum".

Re: how do I compute the MD5 checksum of a string?

Posted: Fri Jul 22, 2016 11:39 pm
by RoryOF
And for instructions type "man md5sum" (no quotes) in a terminal.

Re: how do I compute the MD5 checksum of a string?

Posted: Sat Jul 23, 2016 12:28 am
by yiorisos
Thanx for the quick answer. Sorry, I forgot to write here the basic info: what I m making is an OpenOfficeWriter StarBasic-Macro Programm and no terminal. So what I need is a Macro that generates the MD5 checksum. There is a MD5Thumbprint (https://www.openoffice.org/api/docs/com ... Thumbprint) in the Documentation for OpenOffice, so I suppose you can compute the MD5 in a Open Office macro using some kind of macro code. This is what I'm looking for. I suppose i have to use the XCertificate Interface (https://www.openoffice.org/api/docs/com ... icate.html )in the security module(https://www.openoffice.org/api/docs/com ... le-ix.html), but i don't know how the macro code is looking. So, once more I need a Open Office command or a code like stringMD5=s.md5(), where s will be a string (for example something like "Douglas C-47 Skytrain.jpg") and stringMD5 should be a string or something that I can make to a string (in this example the outcome in the end should be the string "40decfb5f1be8bca1e56c8a853027941", which is the MD5 for the String "Douglas C-47 Skytrain.jpg"). Another possibility is of course to open a terminal through the macro and create there the MD5 or to open a site (like http://www.miraclesalad.com/webtools/md5.php) through a macro and find a way to get the MD5 from command line or the site, but I still have the impression, that there is a simpler way to compute the MD5 with a macro and within the macro. If somebody has a tip, I will be of course very glad! :)

Re: how do I compute the MD5 checksum of a string?

Posted: Sat Jul 23, 2016 12:39 am
by Villeroy
Python can be used as macro language. The default office install includes a Python runtime.
http://stackoverflow.com/questions/5297 ... ng#5297495

Re: how do I compute the MD5 checksum of a string?

Posted: Sat Jul 23, 2016 9:58 am
by yiorisos
Thanks once more. This solution means that I also have to learn how to create a Python macro and then how to use it in the starbasic macro. I had hoped for a more direct solution (and there should be one), but if nothing else comes up, I will use this idea! I wil ask then also in the stackoverflow Forum. Schöne Ferien (in case we dont contact again before winter)!

Re: how do I compute the MD5 checksum of a string?

Posted: Sat Jul 23, 2016 12:37 pm
by Villeroy
Basic is dead, clumsy, difficult. You dont find recent literature on Basic.
Pyton is a professional and modern programming lanuguage (yes, it is easier than Basic).

Stuff like this should be wrapped in a spreadsheet add-in (a package that adds functions to Calc's function wizard). Nevertheless it can be done with pure scripting. The attached spreadsheet document calls a Basic function PYFUNCTION which itself calls a specified Python function in Python module SheetFunctions.py with up to 6 positional arguments.
Save the Python code (embedded Writer object) unmodified as plain text file under
<user_profile>/Scripts/python/SheetFunctions.py
Right-click>Save Copy As...
Automatic file name extension = OFF (or rename SheetFunctions.txt to SheetFunctions.py later)
File Type: Text(*.txt)
Browse to the the Scripts folder and create a subdirectory “python” if necessary and save as “SheetFunctions.py”
Hit Ctrl+Shift+F9 to recalculate the above formulas
Don't forget to turn on the automatic file name extension when you save the next document.
Move the Basic module of the attached spreadsheet to your global Basic container.
menu:Tools>Macros>Organize>Basic... [Organizer...], Ctrl+Drag&Drop copies a module from one library to another library.
From your own Basic code you can call my Basic function PYFUNCTION which will call the right Python code.
PYFUNCTION takes the name of a Python function and up to 6 discrete arguments to be passed to this function (no array arguments yet).

Code: Select all

REM if you put my Basic to a lib other than "Standard":
GlobalScope.BasicLibraries.loadLibrary("LibraryName")

REM let's use a Writer selection of text:
s = ThisComponent.CurrentSelection(0).getString()

md5hash = PYFUNCTION("md5sum", s)
--------- OFF TOPIC ---------
If the code was wrapped in a Calc add-in, the function call would require the installation of an extension package and then:

Code: Select all

REM pass a text selection in Writer to a spreadsheet function:
srv = createUnoService("com.sun.star.sheet.FunctionAccess")
s = ThisComponent.CurrentSelection(0).getString()
md5hash = srv.callFunction("org.villeroy.PyFunctions.getMD5SUM", s)
The function would appear as a normal spreadsheet function MD5SUM among the other spreadsheet functions.

Re: Compute the MD5 checksum of a string with AOO Basic

Posted: Mon Jul 25, 2016 4:24 pm
by yiorisos
thanks a lot for taking all this effort. For the time being i used http://extensions.services.openoffice.o ... nofficeorg, which was enough for me.