Skip to content

Commit 2920c40

Browse files
Add a SHA256 Ada example
1 parent 539a282 commit 2920c40

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

Ada/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
This is a collection of examples using the wolfSSL Ada wrapper
2+
3+
By default it is expecting the wolfssl directory to be located at wolfssl-examples/Ada/wolfssl
4+
but that can be adjusted by changing the directory path for `" with "../wolfssl/wrapper/Ada/wolfssl.gpr";"`
5+
at the top of sha256.gpr.
6+
7+
An example of building for use with SHA256 would be:
8+
9+
```
10+
cd wolfssl-examples/Ada
11+
git clone --depth=1 git@github.com:wolfssl/wolfssl
12+
cd sha256
13+
gprbuild sha256.gpr
14+
```
15+
16+
And then running the example with:
17+
18+
```
19+
bash-3.2$ ./obj/sha256_example
20+
Computing SHA256 hash of: Hello, World!
21+
SHA256 hash: DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F
22+
SHA256 hashing completed successfully!
23+
```
24+
25+
Which matches the hash used from shasum:
26+
27+
```
28+
bash-3.2$ printf 'Hello, World!' | shasum -a 256
29+
dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f -
30+
```

Ada/sha256/sha256.gpr

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
with "../wolfssl/wrapper/Ada/wolfssl.gpr";
2+
3+
project SHA256 is
4+
5+
type OS_Kind is ("Windows", "Linux_Or_Mac");
6+
7+
OS : OS_Kind := external ("OS", "Linux_Or_Mac");
8+
9+
for Languages use ("Ada");
10+
11+
for Source_Dirs use (".");
12+
13+
for Object_Dir use "obj";
14+
15+
for Main use ("sha256_example.adb");
16+
17+
package Naming is
18+
for Spec_Suffix ("C") use ".h";
19+
end Naming;
20+
21+
package Compiler is
22+
for Switches ("C") use
23+
("-DWOLFSSL_USER_SETTINGS", -- Use the user_settings.h file.
24+
"-Wno-pragmas",
25+
"-Wall",
26+
"-Wextra",
27+
"-Wunknown-pragmas",
28+
"--param=ssp-buffer-size=1",
29+
"-Waddress",
30+
"-Warray-bounds",
31+
"-Wbad-function-cast",
32+
"-Wchar-subscripts",
33+
"-Wcomment",
34+
"-Wfloat-equal",
35+
"-Wformat-security",
36+
"-Wformat=2",
37+
"-Wmaybe-uninitialized",
38+
"-Wmissing-field-initializers",
39+
"-Wmissing-noreturn",
40+
"-Wmissing-prototypes",
41+
"-Wnested-externs",
42+
"-Wnormalized=id",
43+
"-Woverride-init",
44+
"-Wpointer-arith",
45+
"-Wpointer-sign",
46+
"-Wshadow",
47+
"-Wsign-compare",
48+
"-Wstrict-overflow=1",
49+
"-Wstrict-prototypes",
50+
"-Wswitch-enum",
51+
"-Wundef",
52+
"-Wunused",
53+
"-Wunused-result",
54+
"-Wunused-variable",
55+
"-Wwrite-strings",
56+
"-fwrapv");
57+
58+
for Switches ("Ada") use ("-g");
59+
end Compiler;
60+
61+
package Linker is
62+
case OS is
63+
when "Windows" =>
64+
for Switches ("Ada") use
65+
("-lm", -- To include the math library (used by WolfSSL).
66+
"-lcrypt32"); -- Needed on Windows.
67+
68+
when "Linux_Or_Mac" =>
69+
for Switches ("Ada") use
70+
("-lm"); -- To include the math library (used by WolfSSL).
71+
end case;
72+
end Linker;
73+
74+
package Binder is
75+
for Switches ("Ada") use ("-Es"); -- To include stack traces.
76+
end Binder;
77+
78+
end SHA256;

Ada/sha256/sha256_example.adb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
with Ada.Text_IO;
2+
with WolfSSL;
3+
4+
procedure SHA256_Example is
5+
procedure Put (Text : String) renames Ada.Text_IO.Put;
6+
procedure Put_Line (Text : String) renames Ada.Text_IO.Put_Line;
7+
8+
use type WolfSSL.Subprogram_Result;
9+
10+
Hash : WolfSSL.SHA256_Hash;
11+
-- Example input data: "Hello, World!"
12+
Input_Bytes : WolfSSL.Byte_Array :=
13+
(1 => 'H', 2 => 'e', 3 => 'l', 4 => 'l', 5 => 'o',
14+
6 => ',', 7 => ' ', 8 => 'W', 9 => 'o', 10 => 'r',
15+
11 => 'l', 12 => 'd', 13 => '!');
16+
SHA256 : WolfSSL.SHA256_Type;
17+
R : Integer;
18+
S : WolfSSL.SHA256_As_String;
19+
begin
20+
Put_Line ("Computing SHA256 hash of: Hello, World!");
21+
22+
-- Create SHA256 instance
23+
WolfSSL.Create_SHA256 (Index => 1, SHA256 => SHA256, Result => R);
24+
if R /= 0 then
25+
Put_Line ("ERROR: SHA256 instance creation failed");
26+
return;
27+
end if;
28+
29+
-- Update with input data
30+
WolfSSL.Update_SHA256 (SHA256 => SHA256, Byte => Input_Bytes, Result => R);
31+
if R /= 0 then
32+
Put_Line ("ERROR: Update of SHA256 instance failed");
33+
return;
34+
end if;
35+
36+
-- Finalize and get hash
37+
WolfSSL.Finalize_SHA256 (SHA256 => SHA256,
38+
Hash => Hash,
39+
Text => S,
40+
Result => R);
41+
if R = 0 then
42+
Put ("SHA256 hash: ");
43+
Put (S);
44+
Put_Line ("");
45+
else
46+
Put_Line ("ERROR: Finalization of SHA256 instance failed");
47+
return;
48+
end if;
49+
50+
Put_Line ("SHA256 hashing completed successfully!");
51+
end SHA256_Example;

0 commit comments

Comments
 (0)