Skip to content

Commit 8bad03c

Browse files
committed
file_checksum: streamline, output char
This is for efficiency
1 parent 1785f51 commit 8bad03c

File tree

4 files changed

+7
-26
lines changed

4 files changed

+7
-26
lines changed

+stdlib/file_checksum.m

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313

1414
function hash = file_checksum(file, method)
1515
arguments
16-
file (1,1) string
17-
method (1,1) string
18-
end
19-
20-
try
21-
hash = string.empty;
22-
catch
23-
hash = [];
16+
file {mustBeFile}
17+
method {mustBeTextScalar}
2418
end
2519

2620
if strcmp(method, "sha256") || strcmp(method, "SHA256")
@@ -36,10 +30,7 @@
3630
end
3731

3832
fid = fopen(file, 'r');
39-
if fid < 1
40-
warning("stdlib:file_checksum:ioerror", strcat("could not open ", file))
41-
return
42-
end
33+
assert(fid > 1, "could not open file %s", file)
4334

4435
while ~feof(fid)
4536
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#update(byte)
@@ -52,10 +43,6 @@
5243

5344
hash = sprintf('%.2x', hash);
5445

55-
try %#ok<TRYNC>
56-
hash = string(hash);
57-
end
58-
5946
end
6047

6148
%!assert(!isempty(file_checksum('file_checksum.m', "sha256")))

+stdlib/md5sum.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
% requires: java
33
function hash = md5sum(file)
44
arguments
5-
file (1,1) string
5+
file {mustBeScalarText}
66
end
77

88
hash = stdlib.file_checksum(file, "md5");

+stdlib/sha256sum.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
function hash = sha256sum(file)
55
arguments
6-
file (1,1) string
6+
file {mustBeScalarText}
77
end
88

99
hash = stdlib.file_checksum(file, "SHA-256");

test/TestHash.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
classdef TestHash < matlab.unittest.TestCase
22

33
properties (TestParameter)
4-
Ph = {{"md5", "5d41402abc4b2a76b9719d911017c592"}, {"sha-256", "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"}}
5-
Pe = {"", "/"}
4+
Ph = {{'md5', '5d41402abc4b2a76b9719d911017c592'}, ...
5+
{'sha-256', '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'}}
66
end
77

88
methods(TestClassSetup)
@@ -34,12 +34,6 @@ function test_hash_text(tc, Ph)
3434
end
3535

3636

37-
function test_hash_empty(tc, Pe)
38-
tc.applyFixture(matlab.unittest.fixtures.SuppressedWarningsFixture("stdlib:file_checksum:ioerror"))
39-
40-
tc.verifyEmpty(stdlib.file_checksum(Pe, "sha256"))
41-
end
42-
4337
end
4438

4539
end

0 commit comments

Comments
 (0)