matlab.lang.obfuscateNames - Obfuscate names of local variables, local functions, and nested

  functions - MATLAB ([original](http://www.mathworks.com/help/matlab/ref/matlab.lang.obfuscatenames.html)) ([raw](?raw))

Obfuscate names of local variables, local functions, and nested functions

Since R2024b

Syntax

Description

matlab.lang.obfuscateNames([inputFile](#mw%5Fb1edaa92-5f16-46c1-bb83-7eaf1f55e88d),[outputFile](#mw%5F2497175a-f023-47b4-9951-4112c491f5eb)) reads MATLAB® source code from inputFile and writes nearly-identical source code to outputFile with the names of local variables, local functions, and nested functions replaced with generic names (id242487092,id581240213, ...). This is intended to provide additional obfuscation for advanced users who are already using [pcode](pcode.html) -R2022a. The names ans,varargin, varargout, and names that are used as literal arguments for the load function are never obfuscated.

Both inputFile and outputFile must be local file system paths and must have a .m filename extension.inputFile must be a function or class definition file. IfinputFile and outputFile are the same file, the file will be overwritten without warning. A warning will be issued if the output filename does not match the name of the function or class being defined.

Source code written to outputFile is encoded using UTF-8.

Note

Code with obfuscated names may behave differently from the original, unobfuscated source code. matlab.lang.obfuscateNames only obfuscates direct occurrences of names in code, such as x in x = 1,if x < 5, and f(x+y). Occurrences in strings, character vectors, and command-syntax arguments are not obfuscated, such aseval("f(x+y)") or clear x.

example

matlab.lang.obfuscateNames([inputFile](#mw%5Fb1edaa92-5f16-46c1-bb83-7eaf1f55e88d),[outputFolder](#mw%5F0d0b07d6-6c68-4f5c-b1f8-79334a870091)) writes the new file in the specified folder with the same name asinputFile. Use "." to write to the current directory.

example

matlab.lang.obfuscateNames(___,[Name=Value](#namevaluepairarguments)) writes the new file with the specified options.

example

Examples

collapse all

Display the contents of the source code file,myfunc.

function outputArg = myfunc(inputArg1,inputArg2) sumVar = inputArg1 + inputArg2; prodVar = inputArg1*inputArg2;

outputArg = sumVar/prodVar;

end

Write myfunc to a new file in the folderObfuscatedCode with local names obfuscated.

matlab.lang.obfuscateNames("SourceCode/myfunc.m","ObfuscatedCode/myfunc.m")

Display the contents of the new myfunc file.

type ObfuscatedCode/myfunc.m

function outputArg = myfunc(inputArg1,inputArg2) id242487092 = inputArg1 + inputArg2; id40037619 = inputArg1*inputArg2;

outputArg = id242487092/id40037619;

end

Display the contents of the source code file,myfunc.

function outputArg = myfunc(inputArg1,inputArg2) sumVar = inputArg1 + inputArg2; prodVar = inputArg1*inputArg2;

outputArg = sumVar/prodVar;

end

Write myfunc with local names obfuscated to the folderObfuscatedCode.

matlab.lang.obfuscateNames("SourceCode/myfunc.m","ObfuscatedCode")

Display the contents of the new myfunc file.

type ObfuscatedCode/myfunc.m

function outputArg = myfunc(inputArg1,inputArg2) id242487092 = inputArg1 + inputArg2; id40037619 = inputArg1*inputArg2;

outputArg = id242487092/id40037619;

end

Display the contents of the source code file,myfunc.

function outputArg = myfunc(inputArg1,inputArg2) sumVar = inputArg1 + inputArg2; prodVar = inputArg1*inputArg2;

outputArg = sumVar/prodVar;

end

Write myfunc to a new file in the folderObfuscatedCode with local names obfuscated.

matlab.lang.obfuscateNames("SourceCode/myfunc.m","ObfuscatedCode/myfunc.m",PreserveArguments=false)

Display the contents of the new myfunc file.

type ObfuscatedCode/myfunc.m

function id242487092 = myfunc(id40037619,id3462717674) id909790545 = id40037619 + id3462717674; id1271760792 = id40037619*id3462717674;

id242487092 = id909790545/id1271760792;

end

Input Arguments

collapse all

Input file, specified by a string scalar or character row vector containing the local file system path.

Output file, specified by a string scalar or character row vector containing the local file system path. If inputFile andoutputFile are the same file, the file will be overwritten without warning. A warning will be issued if the output filename does not match the name of the function or class being defined.

Output folder, specified by a string scalar or character row vector containing the local file system path. To write to a file in the current directory, use".".

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: matlab.lang.obfuscateNames(inputFile, outputFile,PreserveNamesFromLiterals=true)

Preserved names, specified as a string array where each element is a variable, argument, or function name that should not be obfuscated.

Preserve names found in literals, specified as true orfalse. When true, if the name of a variable or function also appears as a command-syntax argument, character vector literal, or string literal anywhere in the file, then that variable or function is not obfuscated. Note that a literal must contain the name only and nothing else.

For example, if PreserveNamesFromLiterals istrue, the name x in the line clear x would be preserved but in the line eval("x+y") the name x would be obfuscated elsewhere in the file.

Preserve arguments, specified as true orfalse. When true, ifinputFile is a function file, then the input and output arguments are excluded from obfuscation. If inputFile is a class definition file, then the method arguments are excluded from obfuscation.

When false, only positional arguments are obfuscated. Name-value arguments are not obfuscated.

Local, nested, and anonymous function arguments are always obfuscated.

Naming pattern, specified as "random" or"natural". If "random", then local names are obfuscated using pseudo-random numbers, which produces names likeid3957197010.

Tips

Version History

Introduced in R2024b