How can I password-protect functions and scripts

13 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Guillaume
Guillaume il 21 Mag 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.

Più risposte (2)

Jose Garcia
Jose Garcia il 21 Mag 2018
Thanks, I'm definitely checking in detail the source control implementation features. I just hadn't heard of them before. I gave it a quick read and it seems to do exactly what I need.

Jan
Jan il 21 Mag 2018
Modificato: Jan il 21 Mag 2018
I'm using a hash inside the M-files. See FEX: DataHash or FEX: GetMD5:
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.

Categorie

Scopri di più su Source Control in Help Center e File Exchange

Prodotti


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by