How can I password-protect functions and scripts
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jose Garcia
am 21 Mai 2018
Kommentiert: Jose Garcia
am 22 Mai 2018
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.
0 Kommentare
Akzeptierte Antwort
Guillaume
am 21 Mai 2018
You can pcode the files and distribute the p files which cannot be edited (by anybody). If the file needs to be modified, then only those with the original m files can modify them. After modification, you'll have to redistribute the new p files.
As plain m files, matlab does not offer any facility to set files read-only as that is a function of the OS.
However, I would say that trying to solve your process problem with technical means within matlab is the wrong approach. For a start, why aren't using version control? With subversion and git having been integrated into matlab for a while now, there's no excuse not to. You can then put out a policy that only unmodified ckeck outs from version control are allowed to be used. You can even modify your code so that with its outputs it also include the version used to generate the outputs (and whether or not it was pristine). I do that with my code.
0 Kommentare
Weitere Antworten (2)
Jan
am 21 Mai 2018
Bearbeitet: Jan
am 21 Mai 2018
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.
Siehe auch
Kategorien
Mehr zu Source Control finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!