How can I password-protect functions and scripts
Show older comments
A group of colleagues and I have put together a group of functions and scripts. Eventually we started having several parallel versions of the program because of non authorized/notified changes in the functions. Is it possible to password-protect the files? I'd like to set something so that all the files can be used and executed but the only way to modify them is if two or more users get together and input their passwords. We're using Matlab R2016b on Windows 10.
I found someone asking the same question in Mathworks but the alternative given for him does not work for me (a backup followed by a comparison and if the files have been modified, they would be restored)
Any suggestion will be appreciated, thanks.
Accepted Answer
More Answers (2)
Jose Garcia
on 21 May 2018
0 votes
function HashMyFile(FileName, Key, Operation)
% File name: File to update or check
% Key: Your password as char vector
% Operation: 'update' or 'check'
Str = fileread(FileName);
CStr = strsplit(Str, '\n');
HashLine = strncmp(Cstr, '% $Hash: ', 9);
CStr2 = cat(2, {Key}, CStr(~HashLine));
Hash = DataHash(CStr2); % Or faster: GetMD5(CStr2, 'Array');
switch lower(Operation)
case 'update' % Update hash:
CStr(HashLine) = {sprintf('%%$Hash: %s', Hash)};
fid = fopen(FileName, 'w');
fprintf(fid, '%s\n', CStr{:});
fclose(fid);
case 'check' % Check hash:
KeyLine = CStr{find(HashLine, 1)};
Hash2 = sscanf(KeyLine(10:end), '%s');
if strcmp(Hash, Hash2)
disp('Hash is correct')
else
disp('File has been modified')
end
end
end
This does not lock the file or hide it, but allows to detect changes. You can update the hash correctly only, if you provide the password.
1 Comment
Jose Garcia
on 22 May 2018
Categories
Find more on Source Control in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!