Skip to content

Commit 9d91f97

Browse files
committed
sha256sum,md5sum: return string instead of char, add tests
1 parent 0bd08fa commit 9d91f97

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

+stdlib/+fileio/md5sum.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@
99
file = expanduser(file);
1010
assert(isfile(file), '%s not found', file)
1111

12-
hash = string.empty;
13-
1412
if ismac
1513
[stat,hash] = system("md5 -r " + file);
1614
elseif isunix
1715
[stat,hash] = system("md5sum " + file);
1816
elseif ispc
1917
[stat,hash] = system("CertUtil -hashfile " + file + " MD5");
2018
else
21-
return
19+
error("no sha256sum method for your OS")
2220
end
2321

24-
if stat ~= 0
25-
return
26-
end
22+
assert(stat == 0, hash)
2723

2824
hash = regexp(hash,'^\w{32}','match','once','lineanchors');
25+
hash = string(hash);
2926

3027
assert(strlength(hash)==32, 'md5 hash is 32 characters')
3128

+stdlib/+fileio/sha256sum.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@
99
file = expanduser(file);
1010
assert(isfile(file), '%s not found', file)
1111

12-
hash = string.empty;
13-
1412
if ismac
1513
[stat,hash] = system("shasum --algorithm 256 --binary " + file);
1614
elseif isunix
1715
[stat,hash] = system("sha256sum --binary " + file);
1816
elseif ispc
1917
[stat,hash] = system("CertUtil -hashfile " + file + " SHA256");
2018
else
21-
return
19+
error("no md5sum method for your OS")
2220
end
2321

24-
if stat ~= 0
25-
return
26-
end
22+
assert(stat == 0, hash)
2723

2824
hash = regexp(hash,'^\w{64}','match','once','lineanchors');
25+
hash = string(hash);
2926

3027
assert(strlength(hash)==64, 'SHA256 hash is 64 characters')
3128

+stdlib/TestFileio.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ function test_copyfile(tc)
142142
tc.verifyThat(fullfile(tempdir, name), IsFile)
143143
end
144144

145+
function test_hash(tc)
146+
import matlab.unittest.constraints.IsFile
147+
148+
fn = tempname;
149+
fid = fopen(fn, "w");
150+
tc.assumeGreaterThan(fid, 0);
151+
fprintf(fid, "hello");
152+
fclose(fid);
153+
tc.assumeThat(fn, IsFile)
154+
155+
tc.verifyEqual(stdlib.fileio.md5sum(fn), "5d41402abc4b2a76b9719d911017c592")
156+
tc.verifyEqual(stdlib.fileio.sha256sum(fn), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
157+
158+
delete(fn)
145159
end
146160

147161
end
162+
end

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ on:
88

99

1010
jobs:
11-
linux:
11+
12+
matlab:
1213
timeout-minutes: 15
1314
runs-on: ${{ matrix.os }}
1415

0 commit comments

Comments
 (0)