This is a very complex question in the current version of Mathematica. There still is no simple way to create distributable code that can be attached easily to a password for each intended user. There are approaches that I have used, but they are complex.
However, if your code is in a .m file and you are willing to create a code distribution for each user that you distribute your code to then you can take the following approach:
Get the user's
$MachineID
.
Use Encode
to create an encoded version of your .m file specifically for that user's $MachineID using
Encode[source, destination, MachineID->"User's MachineID"]
Distribute that custom version. This then will tied to that user's machine. The downside is that if the $MachineID changes (e.g. the user upgrades their machine) then a new distribution will be needed. The user then can load the .m file with Get
.
Alternatively you can use the version of Encode
that has 3 arguments
Encode[source, destination, key]
with a specific key that you create for each user that you distribute the .m file to. You have to generate it for each user. And the user can then load it with
Get[file,key]
And it will work for the user for each computer that they use it on. However the user can then share it with others by providing the key to them. You would have to limit this via a legal license agreement.
Note though that for each of these cases it is still possible for the user to reverse engineer your code using ?
and ??
if the functions are not Protected and Locked. Locking functions has its own set of issues...
There are other approaches that I have created for myself, however they are rather long to describe.
Hopefully WRI will finally create a set of tools for users to market code, but they have not done so as of version 11.