-
Notifications
You must be signed in to change notification settings - Fork 3
/
safe_hw_to_sha.m
56 lines (36 loc) · 1.09 KB
/
safe_hw_to_sha.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function sha = safe_hw_to_sha(hw)
% function sha = safe_hw_to_sha(hw)
str = hw_to_str(hw);
% old code reliant on .Net
% sha = str_to_sha_dotnet(str);
% New code based on Java
sha = str_to_sha_java(str);
end
function str = hw_to_str(hw)
format = '%012.6f';
str = '';
fl = {'x', 'y', 'z'};
fn = {'tau1', 'tau2', 'tau3', 'a1', 'a2', 'a3', 'stim_limit', 'stim_thresh', 'g_scale'};
for i = 1:numel(fl)
for j = 1:numel(fn)
str = cat(2, str, num2str(hw.(fl{i}).(fn{j}), format));
end
end
end
function sha = str_to_sha_dotnet(str) %#ok<DEFNU>
% Original non-java implememntation based on .Net by FSz
sha256hasher = System.Security.Cryptography.SHA256Managed;
sha256 = uint8(sha256hasher.ComputeHash(uint8(str)));
dh = dec2hex(sha256);
sha = dh(:)';
end
function sha = str_to_sha_java(str)
% Extension to non-.Net systems by Maxim Zaitsev
import java.security.*;
import java.math.*;
import java.lang.String;
md = MessageDigest.getInstance('SHA-256');
hash = md.digest(double(str));
bi = BigInteger(1, hash);
sha = char(String.format('%064X', bi));
end