Hiding code in python

How to hide the python code from users?

I am developing a paid application in python. I do not want the users to see the source code or decompile it. How can I accomplish this task to hiding the source code from the user but running the code perfectly with the same performance.

3 Answers 3

To a determined user, you can’t.

From a practical standpoint, you can do some tricks, such as wrapping it into a binary executable or using obfuscation.

There’s no use in doing that.

If your application is not incredibly small and simple, then even if the client (or other programmers they hired) were to work on that code, they’d still need a lot of your help with it.

If it is incredibly small and simple, don’t even bother with it. The client could just get someone to rewrite it from scratch anyway.

I was in a similar situation to this not too long ago. But i managed to find a way to hide my source code, at least, enough to deter most from trying to hack it.

Contrary to common belief, obfuscation and/or encryption of interpreted language scripts is possible. The question is, do you have the time (and motivation) to devote to it? You’ll need to make sure whichever «obfuscation/encryption» method you use is very difficult to crack, and that it does not slow down the execution time of the script in any noticeable way.

Читайте также:  Javascript read property files

If you wish to encrypt a python script, without having to do any work on your end, you can do so using this site.

I tested the following script using the aforementioned site and it produced a very intimidating output, which worked (at least for my purposes):

#!/usr/bin/env python2 # vim: ft=python import sys, json, urllib2 URL = 'http://127.0.0.1:8080/optimization/resources/_harness/status?_fields=health.summary' try: response = urllib2.urlopen(URL).read() except: print('HEALTHCHECKS CRITICAL - Error fetching health check at %s' % URL) sys.exit(2) parsed = json.loads(response) resource = parsed["resource"] if "resource" in parsed else parsed status = resource["health"]["summary"]["status"] incidents = resource["health"]["summary"].get("incidents", []) warning = len([incident for incident in incidents if incident["status"] == "WARNING"]) critical = len([incident for incident in incidents if incident["status"] == "CRITICAL"]) if warning or critical: print('HEALTHCHECKS %s - %d WARNING - %d CRITICAL' % (status, warning, critical)) else: print('HEALTHCHECKS OK - no incidents') print(json.dumps(resource, indent=2)) sys.exit(.get(status, 3)) 

Can the obfuscated code produced be broken into? Maybe, maybe not. But it is sure to deter most people from even attempting it.

If you do have some time on your hands and wish to encrypt your script on your own using your own improvised method, you’ll want to use the openssl command. Why? Because it appears to be the one encryption tool that is available across most Unix systems. I’ve verified it exists on Linux (ubuntu, centos, redhat, mac), and AIX. SunOS i believe uses the «crypt» command instead.

The simplest way to use Openssl to encrypt a script is:

1. cat | openssl aes-128-cbc -a -salt -k "specify-a-password" > thescript.enc OR 2. openssl aes-128-cbc -a -salt -in -k "yourpassword" 

To decrypt a script using Openssl (notice the ‘-d’):

1. cat thescript.enc | openssl aes-128-cbc -a -d -salt -k "specify-a-password" > thescript.dec OR 2. openssl aes-128-cbc -a -d -salt -in -k "yourpassword" > thescript.dec 

The trick here would be to automate the supply of password so your users need not specify a password each time they wanted to run the script.

Источник

How to hide code in Jupyter Notebook?

Run Mercury to convert Jupyter Notebooks to web apps

Hide Code in Jupyter Notebook banner

Your analysis is ready. You would like to share results from Jupyter Notebook with non-technical users. You have to hide the Python code to not scare them. There are several ways in which it can be achieved.

1. Hide code when exporting notebook with nbconvert

The nbconvert is a perfect tool for exporting Jupyter Notebook to other formats, like HTML or PDF. It has argument —no-input to hide the code in the exported notebook. The example command that converts ipynb file to HTML:

jupyter nbconvert --to html --no-input my-notebook.ipynb 

The above command with produce my-notebook.html file without code. If you decide to show the code but would like to hide the prompt, you can use the —no-prompt argument:

jupyter nbconvert --to html --no-prompt my-notebook.ipynb 

2. Hide selected cells when exporting notebook with nbconvert

What if you would like to hide only selected cells? You can add tag for cell that has code that you would like to hide. It is simple. Please click View -> Cell Toolbar -> Tags . Please add a tag hide_cell (it can be any string) to the selected cell. The animation below shows how it can be done.

You need to pass the hide_code tag in the nbconvert command:

jupyter nbconvert --to html --TagRemovePreprocessor.remove_cell_tags='' my-notebook.ipynb 

The above command will produce the my-notebook.html file with removed code for all cells with hide_code tag.

3. Hide code with custom JS code

What if you would like to share a notebook with `nbviewer and you don’t want to show the code. There is no built-in feature for that. There is a custom JS/HTML code snippet that can be inserted in the first cell of the notebook (link to stackoverflow response with code snippet).

Please insert the below code at the beginning of the notebook

from IPython.display import HTML HTML('''  
''')

The bolow animation showing how to toggle code:

4. Hide code and share notebook with Mercury

The Mercury is an open-source framework that makes Jupyter Notebooks sharing simple. It converts notebook into web application that can be served as app, dashboard, report, presentation or REST API. It requires the YAML header in the first cell of the notebook. The example YAML header:

--- title: My notebook description: My amazing notebook with hidden code show-code: False --- 

There is a parameter show-code that controls whether to show or hide the code in the resulting notebook. What is more, the interactive widgets can be added to the notebook with YAML. They are directly connected to the variables in the Python code. Below there is an example notebook with YAML header and the web application published with Mercury .

Notebook with YAML header

The web application created from Jupyter Notebook:

The Mercury allows user to tweak widgets parameters and execute notebook with new values. The final notebook can be downloaded as HTML or PDF file with one click. You can read more about Mercury in the article how to share Jupyter Notebook with non-programmers.

Summary

Most people are confused when seeing many lines of code. Hiding the code when sharing the Jupyter Notebook with non-coders is a good idea. There are several ways to achieve that. The nbconvert tool offers a —no-input argument and TagRemovePreprocessor for exporting notebooks without code. There is a custom code snippet in JS/HTML that can be used in the notebook to toggle the code. The Mercury framework has a show-code parameter to control code display.

Источник

Data Hiding in Python

In this article, we will discuss data hiding in Python, starting from data hiding in general to data hiding in Python, along with the advantages and disadvantages of using data hiding in python.

What is Data Hiding?

Data hiding is a concept which underlines the hiding of data or information from the user. It is one of the key aspects of Object-Oriented programming strategies. It includes object details such as data members, internal work. Data hiding excludes full data entry to class members and defends object integrity by preventing unintended changes. Data hiding also minimizes system complexity for increase robustness by limiting interdependencies between software requirements. Data hiding is also known as information hiding. In class, if we declare the data members as private so that no other class can access the data members, then it is a process of hiding data.

Data Hiding in Python:

The Python document introduces Data Hiding as isolating the user from a part of program implementation. Some objects in the module are kept internal, unseen, and unreachable to the user. Modules in the program are easy enough to understand how to use the application, but the client cannot know how the application functions. Thus, data hiding imparts security, along with discarding dependency. Data hiding in Python is the technique to defend access to specific users in the application. Python is applied in every technical area and has a user-friendly syntax and vast libraries. Data hiding in Python is performed using the __ double underscore before done prefix. This makes the class members non-public and isolated from the other classes.

Источник

3 ways to hide code in Jupyter Notebook Presentation

Run Mercury to convert Jupyter Notebooks to web apps

Hide code in Jupyter Notebook Presentation

Creating a Jupyter Notebook presentation is a great way to share your data-rich results. All your plots and results are already in slides. There is no need to copy results between Jupyter Notebook and software for presentation manually. The slideshow is a preferred way to share results with non-technical stakeholders. The code in the slides might scare them. How to hide code in a presentation generated from Jupyter Notebook? We will show you 3 approaches to hiding code in the slides.

Use nbconvert

The nbconvert command line tool can be used to convert Jupyter Notebook to presentation slides. It has an option —no-input to hide the input code. The command to convert Jupyter Notebook to the presentation:

jupyter nbconvert --to slides --no-input my-notebook.ipynb 

The above command will produce a my-notebook.slides.html file with a Reveal.js presentation.

If you would like to serve the presentation locally without the code, use:

jupyter nbconvert --to slides --no-input --post serve my-notebook.ipynb 

Hide-code extension

There is a hide_code extension. It can be used to hide code in cells selectively. The nice thing about the extension is that it works with the nbconvert and a RISE extension for slide development.

Installation steps in the virtual environment (added —user parameter):

pip install hide_code jupyter nbextension install --py --user hide_code jupyter nbextension enable --py --user hide_code jupyter serverextension enable --py --user hide_code 

Please switch on the Hide code extension in the cell by clicking View -> Cell Toolbar -> Hide Code . Each cell will have a toolbar where you can click a checkbox to show or not a cell in the output notebook.

Enable hide_code extension

The hide_code extension can be used with a nbconvert tool with a hide_code_slides argument passed:

jupyter nbconvert --to hide_code_slides my-notebook.ipynb 

Hide code in Mercury

The Mercury open-source framework can be used to create parameterized presentations. It uses the YAML header to add interactive widgets to the notebook. There is a show-code parameter, which decides about displaying the code.

--- title: Parametrized Presentation 📊 description: Presentation with widgets output: slides show-code: False # the rest of parameters . --- 

The YAML header is added in the first cell of the notebook. The Mercury serves the notebook as a web application. The output can be a web application, dashboard, report, or presentation.

An example of the parameterized presentation created with Jupyter Notebook and Mercury:

Summary

Not everyone is a code lover. Developers using Jupyter Notebook to create slides need to hide the code in the presentation. Slides with hidden code don’t scare non-technical users and still have all plots and results. The Jupyter Notebook with code and results can be committed to a code repository, where tech-savvy users can view them.

Источник

Оцените статью