Main Content

matlab.lang.obfuscateNames

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

Since R2024b

    Description

    matlab.lang.obfuscateNames(inputFile,outputFile) 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 -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. If inputFile 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 as eval("f(x+y)") or clear x.

    example

    matlab.lang.obfuscateNames(inputFile,outputFolder) writes the new file in the specified folder with the same name as inputFile. Use "." to write to the current directory.

    example

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

    example

    Examples

    collapse all

    Display the contents of the source code file, myfunc.

    type SourceCode/myfunc.m
    function outputArg = myfunc(inputArg1,inputArg2)
        sumVar = inputArg1 + inputArg2;
        prodVar = inputArg1*inputArg2;
    
        outputArg = sumVar/prodVar;
    end
    

    Write myfunc to a new file in the folder ObfuscatedCode 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.

    type SourceCode/myfunc.m
    function outputArg = myfunc(inputArg1,inputArg2)
        sumVar = inputArg1 + inputArg2;
        prodVar = inputArg1*inputArg2;
    
        outputArg = sumVar/prodVar;
    end
    

    Write myfunc with local names obfuscated to the folder ObfuscatedCode.

    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.

    type SourceCode/myfunc.m
    function outputArg = myfunc(inputArg1,inputArg2)
        sumVar = inputArg1 + inputArg2;
        prodVar = inputArg1*inputArg2;
    
        outputArg = sumVar/prodVar;
    end
    

    Write myfunc to a new file in the folder ObfuscatedCode 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 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.

    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

    Specify optional pairs of arguments as Name1=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 or false. 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 is true, 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 or false. When true, if inputFile 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 like id3957197010.

    • "random" — Local names are obfuscated using pseudo-random numbers, which produce names like id3957197010. matlab.lang.obfuscateNames generates pseudo-random numbers deterministically and if this function is run multiple times on a source-code file then the same numbers will be generated each time.

    • "natural" — Local names are obfuscated, starting with the name closest to the beginning of the file, in sequential order like id1, id2, etc.

    Tips

    • Thoroughly test obfuscated code to ensure it is functioning as intended. Consider making use of MATLAB Testing Frameworks to write automated tests to verify the behavior of obfuscated code. If the code does not behave as desired, use PreserveNames and other name-value arguments to control the obfuscation process.

    • Use name obfuscation together with pcode -R2022a and mcc -s. Name obfuscation preserves comments to make debugging easier. Obfuscated source code should not be distributed directly; it should be distributed as a P-code file, ideally in a deployable form produced by the MATLAB Compiler or MATLAB Compiler SDK.

    • Do not write code that relies on specific obfuscated names. Instead, use the PreserveNames option exclude names from obfuscation. Names assigned after obfuscation may change in the future.

    Version History

    Introduced in R2024b