Argon2Hashing
This sample adds modern password hashing to PowerBuilder. It is a small C# assembly that does Argon2 (the winner of the Password Hashing Competition and the algorithm OWASP recommends first), shaped so it imports cleanly with the .NET DLL Importer. It comes with a PowerBuilder 2025 demo app that uses it.
Source code: https://github.com/bruce-armstrong/Argon2Hashing (MIT license)
What's included
- Argon2Hashing.dll: Argon2id, Argon2i and Argon2d (RFC 9106, version 1.3) in plain managed C#. It has no NuGet dependencies, so you deploy one DLL. It is built for both .NET 8 and .NET Framework 4.8.
- Standard hash strings. Hashes use the PHC format, for example
$argon2id$v=19$m=65536,t=3,p=4$<salt>$<hash>. That is the format libargon2, PHPpassword_hash(), Python argon2-cffi, Node argon2 and others use, so hashes move freely between PowerBuilder and those systems. - Argon2Demo.pbsln: a PowerBuilder 2025 solution with the imported proxy object (
nvo_argon2hasher) and a demo window. In the window you can hash a password, verify one, change the settings and check whether a stored hash needs upgrading. - Tests. The library is checked against the official RFC 9106 test vectors and the libargon2 reference hash. The demo exe also has a
/selftestswitch that tests the whole path from PowerBuilder through the proxy into .NET, in both 32-bit and 64-bit builds.
Why Argon2?
Older approaches such as MD5, SHA-1 or SHA-256, even with a salt, are fast. That makes them easy to brute-force with GPUs. Argon2 is designed to be slow and to use a configurable amount of memory, which makes large-scale guessing expensive. Each hash gets its own random salt, and the salt and settings are stored inside the hash string itself, so you only need one varchar(255) column per user.
Using it from PowerScript
nvo_argon2hasher lnv_argon2
lnv_argon2 = create nvo_argon2hasher
// Registration: store ls_hash (about 100 characters)
ls_hash = lnv_argon2.of_hashpassword(ls_password)
// Login
if lnv_argon2.of_verifypassword(ls_password, ls_stored_hash) then
if lnv_argon2.of_needsrehash(ls_stored_hash) then
// settings were raised since this hash was made: upgrade it
ls_new_hash = lnv_argon2.of_hashpassword(ls_password) // save ls_new_hash
end if
// logged in
end if
The defaults are argon2id, 64 MiB of memory, 3 iterations, 4 lanes. That is the second recommended setting in RFC 9106, and it takes about 0.2 seconds per hash on a modern PC. You can change it with of_setparameters(). For example, of_setparameters(19456, 2, 1) is OWASP's lower-memory minimum.
Making a C# class the .NET DLL Importer likes
The main lesson from this project is how to shape the C# class so the importer handles it without surprises. Argon2Hasher follows these rules:
- It is a public, non-static class with a public parameterless constructor.
- It has public instance methods only. There are no overloads, properties, generics, optional parameters,
out/refparameters or events. - Signatures use only
string,intandbool, which map to PowerBuilderstring,longandboolean. - Methods never throw. On failure they return
""orfalse, andGetLastError()gives the reason. The proxy'sue_errorevent andis_ErrorTextthen only matter when the assembly can't be loaded at all. - Settings are plain setter and getter methods (
SetParameters,GetMemoryKiB, ...) rather than properties.
Importing it into your own app
- Build the DLL (
dotnet build dotnet\Argon2Hashing -c Release) or runbuild.ps1. - In PowerBuilder go to Tools > .NET DLL Importer.
- Pick one build:
bin\Release\net8.0\Argon2Hashing.dllwith framework .NET Core (PB 2022 R2 and later). The proxy usesLoadWithDotNetCore.bin\Release\net48\Argon2Hashing.dllwith framework .NET Framework (PB 2019 R3 and later). The proxy usesLoadWithDotNetFramework, and nothing extra needs installing on Windows.
- Select class
Argon2Hashing.Argon2Hasherand all of its methods, then choose your target library. - Deploy
Argon2Hashing.dllnext to your exe. With the .NET 8 build, also deployArgon2Hashing.deps.jsonand make sure the .NET 8 runtime for your app's bitness is installed (x86 for a 32-bit exe).
API summary
| Proxy function | Purpose |
|---|---|
of_hashpassword(password) |
Hashes with a new random salt. Returns the PHC string, or "" on error. |
of_verifypassword(password, hash) |
Checks a password. Reads the variant, settings and salt from the hash and compares in constant time. |
of_needsrehash(hash) |
True if the hash was made with different settings than the current ones. |
of_isvalidhash(hash) |
True if the text is a well-formed Argon2 hash. |
of_setparameters(memoryKiB, iterations, parallelism) |
Sets the cost settings. Returns false if a value is out of range. |
of_setvariant("argon2id" | "argon2i" | "argon2d") |
Chooses the variant (argon2id is recommended). |
of_sethashlength(bytes), of_setsaltlength(bytes) |
Hash length 16 to 512 bytes (default 32). Salt length 8 to 64 bytes (default 16). |
of_setsecret(pepper) |
Optional server-side secret. It is not stored in the hash, so set the same value before verifying. |
of_getlasterror() |
Reason for the last failure, or "". |
Running the demo
Clone the repository and run pwsh -File build.ps1 -Test. This builds the DLL, runs the .NET tests, compiles the PowerBuilder app with PBAutoBuild into build\argon2demo\ and runs the end-to-end self-test. Add -Platform 64 for a 64-bit exe. You can also open Argon2Demo.pbsln in PowerBuilder 2025 and run it from the IDE. build.ps1 copies the DLL into the solution folder, where the IDE looks for it.
Feedback and pull requests are welcome on GitHub.
This message has an attachment file.
Please log in or register to see it.
Please Log in or Create an account to join the conversation.