Docsity
Trusted Seller

IT Coding, Tools and Security - Prof. Tessera/Barbierato - ITEM, Dispense di Programmazione e Tecnologie Web

Toggle sidebar
bg1
1
Appunti Salvatore De Lellis – Innovation and Technology Management anno 2021/2022
IT Coding, Tools and Security – Module A – Tessera and Barbierato
Computational thinking
The processes involved in formulating problems and their solutions so that the solutions are represented in
a form that can be effectively carried out by an automated information-processing system. What is not?
1. Just another hardware/software technical skill.
2. Something to add to my curriculum vitae.
3. Not a procedural approach.
We have just two tools in computational thinking:
1. Abstraction, that is the ability to filter out information that is not necessary to solve a certain type of
problem and generalise the information that is necessary, focus on the core of the problem, taking
out details, so that my solution can work for different situations.
2. Generalisation, that is the formulation of general concepts from specific instances by abstracting
common properties.
Coding
Coding or computer programming is the process of designing and building a computer program (a sequence
of simple steps that can be executed by the computer) to perform a specific task. Many programming
languages and techniques exist. At the root of programming there is the concept of algorithm as an abstract
way for describing the process used to solve a problem. Algorithms represent the idea that drive us to
develop specific solutions for some classes of problems. Coding is the representation of an algorithm in a
specific computer programming language.
Algorithms
It’s like a cooking recipe: it must be followed exactly, it must be unambiguous, and it must have an end. An
algorithm is an ordered set (we must define the precedence among steps, specifying that some steps must
be executed before others) of unambiguous (must have a unique and well-defined interpretation, not
depending on something not written in your algorithm), executable steps (for executing the steps is not
required any creative skill because everything is already well-defined) that defines a terminating process
(that cannot run forever).
It’s important to emphasize the distinction between an algorithm and its representation, a distinction that is
analogous to that between a story and a book. A story is abstract, or conceptual, in nature, a book is a physical
representation of a story. If a book is translated into another language or republished in a different format,
it’s merely the representation of the story that changes, the story itself remains the same. There are two
standards to describe algorithms:
1. Pseudocode: it’s a plain language description of the various steps of an algorithm. Pseudocode often
uses structural conventions (semantic building blocks) of a normal programming language but is
intended for human reading rather than machine reading. It typically omits details that are essential
for machine understanding of the algorithm (programming language specific directives).
2. Flowchart: it’s a type of diagram that represents a workflow or process. A flowchart depicts in a
diagram the various steps executed by an algorithm, can also be defined as a diagrammatic
representation of an algorithm, a step-by-step approach to solving a task. Make use of terminal
elements that are start and stop and end tag that highlight the start and termination.
Primitives
To describe algorithms we use building blocks, describing various actions, called primitives. They represent
very common semantic structures for example:
1. Execution statement: simply indivisible execution step.
pf3
pf4
pf5
pf8
pf9
pfa

Anteprima parziale del testo

Scarica IT Coding, Tools and Security - Prof. Tessera/Barbierato - ITEM e più Dispense in PDF di Programmazione e Tecnologie Web solo su Docsity!

1

IT Coding, Tools and Security – Module A – Tessera and Barbierato

Computational thinking The processes involved in formulating problems and their solutions so that the solutions are represented in a form that can be effectively carried out by an automated information-processing system. What is not?

  1. Just another hardware/software technical skill.
  2. Something to add to my curriculum vitae.
  3. Not a procedural approach. We have just two tools in computational thinking:
  4. Abstraction , that is the ability to filter out information that is not necessary to solve a certain type of problem and generalise the information that is necessary, focus on the core of the problem, taking out details, so that my solution can work for different situations.
  5. Generalisation , that is the formulation of general concepts from specific instances by abstracting common properties. Coding Coding or computer programming is the process of designing and building a computer program (a sequence of simple steps that can be executed by the computer) to perform a specific task. Many programming languages and techniques exist. At the root of programming there is the concept of algorithm as an abstract way for describing the process used to solve a problem. Algorithms represent the idea that drive us to develop specific solutions for some classes of problems. Coding is the representation of an algorithm in a specific computer programming language. Algorithms It’s like a cooking recipe: it must be followed exactly, it must be unambiguous, and it must have an end. An algorithm is an ordered set (we must define the precedence among steps, specifying that some steps must be executed before others) of unambiguous (must have a unique and well-defined interpretation, not depending on something not written in your algorithm), executable steps (for executing the steps is not required any creative skill because everything is already well-defined) that defines a terminating process (that cannot run forever). It’s important to emphasize the distinction between an algorithm and its representation, a distinction that is analogous to that between a story and a book. A story is abstract, or conceptual, in nature, a book is a physical representation of a story. If a book is translated into another language or republished in a different format, it’s merely the representation of the story that changes, the story itself remains the same. There are two standards to describe algorithms:
  6. Pseudocode: it’s a plain language description of the various steps of an algorithm. Pseudocode often uses structural conventions (semantic building blocks) of a normal programming language but is intended for human reading rather than machine reading. It typically omits details that are essential for machine understanding of the algorithm (programming language specific directives).
  7. Flowchart: it’s a type of diagram that represents a workflow or process. A flowchart depicts in a diagram the various steps executed by an algorithm, can also be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task. Make use of terminal elements that are start and stop and end tag that highlight the start and termination. Primitives To describe algorithms we use building blocks, describing various actions, called primitives. They represent very common semantic structures for example:
  8. Execution statement: simply indivisible execution step.

2

  1. Conditional primitive: in which you have to alter the control flow that is the order in which various statement are executed. In some situations, there are some statements that can be executed only if some conditions hold (if statement).
  2. Iterative primitive: another common semantic structure is the repeated execution of a statement as long as some condition remains true (while primitive). Usually, data processing requires to repeat the same operation with different data. A collection of primitives along with a collection of rules stating how the primitives can be combined to represent more complex ideas constitutes a programming language. Each primitive has its own syntax (how we can write the primitive) and semantics (refers to the meaning of the action executed by the primitive). Primitive and abstract data type
  3. Primitive data type (the usual ones): Numbers, Boolean, Strings, Dates, Time, Enumerated. Primitive data types are those supported directly by the language and are not composed of any other data types.
  4. Abstract data type: the term abstract refers to the fact that these data structures do not exist physically (remember that a computer sees only bits), but they are a convenient way for us to visualise data (Lists, Tuples, Stack, Graphs, Trees). Abstract data types are created by users, and in libraries. Scope of the variable The scope of the variable is the part of the program where the variable is accessible (its visibility). In other words, which parts of your algorithm/program can see the variable or use it. We have two different kinds of scope of variables: 1. Spatial (parts of code): portion of code in which I can make some reference. 2. Temporal (time window of the execution time): the time window in the execution time in which variables exist. Most common activities are already coded and available as functions. Functions are stored in library (packages in python) and can be made available (imported) to our programs. Boolean data type Boolean data types are logical data type that can have only two values (true or false). So, we can for example compare 10 with 1, saying if it’s true or not. But we can also compare the content of two variables, using the double quotes to specify that are string values. Boolean expression can be very complex. Every computer program can be expressed in terms of Boolean expressions. We can describe the behaviours of program through Boolean expression but are very long and hard to understand so high level directives are preferred. Why? Because of the abstraction principle, one of the two basic principles (abstraction and generalisation). Boolean operators are: There are also other operations in Boolean algebra:

4 created the packages and you just have to use the combining properly what they offer. Languages used to this end are Python, R, Perl, Javascript, etc. The learning curve is very soft. Program unit (Python function) Program unit is an abstract concept using algorithm description. In many programming languages, program unit has the same meaning as function. A function is a block of organised, reusable code that is used to perform a single, related action. It allows us to define new reusable abstract tool. Cut and paste a code is not a good idea because if we realise that the code has some problem, we will have to fix in all the positions. On the contrary, define and call the function for solving the problem, once the function is fixed, all the code using the function will apply the corrected version of it. A function is a block of code which only runs when it’s called. Open a file (read/write) The first step consists of declaring to Python interpreter what you want to do with a file (write/append it or read it). From a Python perspective, this corresponds to open the file in a way or the other. Each time you open a file in one of these three ways (write, append or read), Python returns a handle, which is a unique reference to a specific file open in a particular way. Once you have a handle, you can use it to perform the intended operation (write, append or read). Finally, since you have opened a file, you must close it (forgetting the last step might cause problems with the file). Packages Functions are usually collected in a library, named packages. Packages can be imported in your Python environment to add abstract tools (already coded functions) so we can just use them without the need of defining them. For example, mathematical functions. List Sometimes I have to consider collections of elements. Lists are ordered collections of elements. Ordered mean that we have a position for each element. The declaration syntax uses [ ] for defining the begin and the end of the list. The first element is called head and the last is called tail. Lists can contain duplicates. Also, they are heterogeneous, so they are not limited to contain elements of the same data type. Lists can also be empty so not containing elements. Lists are mutable. If we imagine a lists as a collection of boxes, each one associated with a variable and box size is related to the amount of information stored in the box itself, we could need an operator to access to individual elements inside the list. This is called slice operators. They allow the access to the individual box inside the list. Slice operator syntax is to use listName[position] so the name of the variable in the list and the square brackets to identify its position. The position is an integer ranging from 0 up to the n - 1. Methods Methods are object-oriented concepts. Lists are stored as objects in Python. Methods are functions applied to given data structures. Syntax: name.methodName(optional params), here I am saying I want to apply some special function just to this object. This concept comes from object-oriented paradigm. The presence of optional parameters changes the actions executed by the method itself (this behaviour is named polymorphism). List methods A = [‘a’, ’b’, ‘c’, ’d’]

5

1. Append: will add an additional element to the tail of the list. 2. Pop (): will remove the last element of the list. 3. Pop (n): remove the element in position n. 4. Insert (element, n): add an element in position n. 5. Extend (otherList): will provide a list adding it to the first one. 6. Clear (): remove all the elements from the list. Tuples They are a convenient way to regroup the primitive values we already know (booleans, strings). A tuple is an abstract data structure composed of heterogenous elements. Properties of all tuples:

  1. It’s immutable (not modifiable). The convenience in having a data structure that is not mutable is that it is optimised (because it cannot be modified). If you know in advance that you will not edit your elements, it will be more efficient. On the other hand, if you think you are going to change the content of the data collection is better to use a list. **2. Each element of a tuple of n elements has a position, starting from 0 to n-1.
  2. Processing tuples is faster that processing lists.** Tuples have a nice feature called packing. Another option is sorting the data structure by using the sorted function: pint(sorted(tup)) will just sort the elements in ascending order. But though, you cannot change the content of a tuple. Sorted return a list (see the square brackets) and so it’s not a tuple anymore. Sets Sets are unordered ( we cannot apply the slice operator ) data collections of unique elements (duplicated elements are discarded). They are not indexed , so no positions and nor editable. You can browse and print all the elements using a for cycle. Set (like any other thing in Python) are objects and have methods: Dictionary Dictionaries are data structures that allows to combine two values typically a key and a value. Dictionary is an ordered data structure. It’s mutable so you can change your elements as much as you please. It’s indexed , you can access by using indexes, though the best way is by key. In a dictionary, each element is a couple of <key, value>. In tuples and sets you have just one element. You can browse and print the keys only of a dictionary using a for each loop. It’s similar to List, Set, and Tuples. However, it’s not indexed by a sequence of numbers but indexed based on keys and can be understood as associative arrays. On an abstract level, it consists of a key with an associated value. Keys are unique within a dictionary and cannot be duplicated inside a dictionary. In case if it’s used more than once then subsequent entries will overwrite the previous value. DataFrame A DataFrame is a two-dimensional array of values with both a row and a column index. DataFrame are size- mutable and potentially heterogeneous data. List can be altered. Also provide some features for indexing that is getting some data: there is a function called set_index that says I have a data frame and I want to use a different column for indexing, for example the index column. I can also put a multilevel index.

7 depend on the algorithm we are considering. In other words, the number of operations depends on some properties of dataset. Most common properties of data set are:

  1. Nearly ordered: most of the data are already in the correct position and there are only some elements to be adjusted.
  2. A few unique numbers: list contains many duplicated elements.
  3. Already sorted dataset: ordered algorithm to show how to handle semantic situations. One of the most common techniques is the bubble sort algorithm. The bubble sort is a very simply algorithms that compares each pair of adjacent elements and swap the two elements if they are out of order. This process is iterated until a scan of all element pairs do not perform any swap. If I process all the elements of the list without making swaps, then I can assume data are ordered. Bubble sort complexity There are different situations. The best case is when data is already sorted. In this case the computational complexity is O(n), a number of comparison proportional to the number of elements. The worst case is when the data is sorted in the opposite order (from larger to smaller). So, in the worst case the complexity is: (n − 1 ) + (n − 2 ) + ⋯ + 1 ≈ O(n!). In the worst case the complexity of the bubble sort algorithm is equal to the complexity of the selection sort, but in the best case it’s much smaller because grows linear with n. The average case is a mixture between O(n) and O(n^2 ). Although in the worst case the selection and the bubble sort algorithm have the same computational complexity, for nearly sorted data the bubble sort is incredibly faster. If we apply the bubble sort to a list that contains 1000 elements nearly ordered, we are going to spend time to execute 1000 statement. On the contrary, if we are using the selection sort algorithm, we are going to make 1000 x 1000 = 1 million of operations. Insertion sort Bubble sort causes bad performance when data are not ordered and are in total chaos. Insertion sort is more intuitive, and it does mostly what a human would do to order the data. It’s a relatively simple algorithm for sorting an array. It is an in-place algorithm, that is, it sorts the array without having to create a copy of it, saving memory. While it is much less efficient than more advanced algorithms, it can have some advantages: for example, it is simple to implement and is efficient for starting sets that are nearly sorted. Recursion The idea is that I want to solve a large problem, so I break it down in smaller problems and then I am applying exactly the same approach to the smaller problems, then I combine the solutions of the smaller problems to get the one of the large problem. Recursion is the capability of a function to invoke itself using its dataset. Recursion solves such recursive problems by using functions call themselves from within their own side. Time Sometimes we have to perform calculation on time. As far as we know, if we want to represent a date, we must use a string. The problems arise if we want to perform some math on this variable, for example calculate how many days have elapsed between today and that date. In order to deal with time, Python must use a package called time. This package contains different functions. One of these is time(), which returns the number of seconds elapsed from an instant of time called epoch corresponding to January, the 1st 1970. A function called ctime(seconds) converts seconds elapsed from epoch and convert them into a string. NumPy NumPy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays, used by a lot of packages for data structure because of the high level of performance. Also provide tools that are functional for working with arrays and facilities at

8 the core of other packages. With NumPy you can calculate the solution of system of two equations with two incognitos, but also a polynomial (poly1d). The abstract data structure offered by Numpy is the array. The core of NumPy is well-optimised C code. That is why they provide very good performances. Usually interpreted languages like python suffer of poor performance because every time the interpreter has to process everything before executing and the idea is just that we have optimised libraries where there are some functions that call other functions written in different programming languages. So, we can call easily those function from python, and those functions provide very good performances. Array performance An array is a collection of homogeneous data items stored in contiguous memory locations. An array can also be defined as an ordered collection of homogeneous items indexed by contiguous integers. Arrays grant better performance by indexing because they exploit homogeneous data (each element requires the same amount of memory). Many programming languages provide array as native data type, but python doesn’t. They are implemented in a package called NumPy and it’s used at the basis of many other packages. Array vs list Arrays need to be declared. Lists don't, since they are built into Python. Creating an array, on the other hand, requires a specific function from NumPy package (numpy.array()). Because of this, lists are used more often than arrays. Arrays can store data very compactly and are more efficient for storing large amounts of data. Arrays are great for numerical operations. Lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code. If you try the same with a list, you'll get an error. You cannot change the size of an array. If you want to, you need to copy your array inside another one of a different size. You can’t insert elements inside of an array because they are contagious. So, they are less flexible than lists and tuples but provide a better performance. They are very efficient as the number of elements is increased. Array shapes There are different ways to organise an array. The syntax is similar to the syntax used for lists [ ], but another popular way to organise data are matrix. Matrix is a two-dimensional structure in which data are organised like in a table, and when we need to pick up some elements, we need to specify the index of the row and the index of the column. Using Numpy, we can specify as many dimensions as we need. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension. Very popular NumPy functions they provide the most common needs of a programmer:

  • Creates an array initialise all the elements to zero.
  • Makes the same thing but initialise the elements to one-creates an array that contains consecutive numbers up to what you have specified in the brackets.
  • Similarly, we can specify start end and the increment between the numbers.
  • Linear apace means I have a range from start to end, and I want all the positions for a given number of points “I want to subdivide a range from zero to ten in five points”. Pandas Pandas is a fast, powerful, flexible and easy to use open-source data analysis and manipulation tool, built on top of the Python programming language. It provides fast, flexible, and expressive data structures designed to make working with “relational” or “labelled” data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python. It provides a way to store, manage and handle tabular data with heterogeneously typed columns, as in an SQL table or Excel spreadsheet. Provides facilities to deal ordered and unordered (not necessarily fixed frequency) time series data, and also arbitrary matrix data (homogeneously typed or heterogeneous) with row and column labels.

10 strong encryption system. Brute force decryption (try each key) taking 1 sec on DES, takes 149 trillion years for AES 256 bit. Session key Session key is used to exploit the advantages of both symmetric and public key cryptography techniques. We restrict the use of public key cryptography to exchange only the value of the symmetric key that we use for encrypting large messages. We take advantages of both techniques: public key cryptography doesn’t require sharing secrets so we can securely communicate and transmit the value of a key, and symmetric key which is a session key because for each message is randomly generated. RSA: Creating public/private key pair The idea is that we need to choose large prime numbers p and q. Then we compute the product and then compute other quantities related to p and q. The important property is that we have a couple of numbers (n and e, public encryption key) and another couple (n and d, private key) that are mathematically related by the previous relationships. Prime numbers factorisation is an incredible time-consuming activity that has an exponential computational complexity. We have two important properties:

  1. When we encrypt the message, if we apply the correct private key, we will be able to recover the message.
  2. We can switch the order in which we apply private and public key. This property is important because guarantee confidentiality of the message. It comes from modular arithmetic. Why is RSA secure? Because it’s very difficult to factorise big numbers. Digital signature Since it’s not forgeable, we can’t have any kind of digital signature based on symmetric cryptography. In symmetric cryptography both participants know the private key, so both are able to generate whatever we are using for digital signing. The idea is that we are using public key cryptography. Computational safe encryption system A system where cost for decrypting a message is much larger that the message value. The time required to break the code is much longer that the security temporal scope, that is protecting the confidentiality of your message for a defined time. Hash – Messages digests Directly applying public key cryptography to large messages can require huge amounts of resources. Apply hash function H to m, get fixed size message digest, H(m). The idea is to use a function called hash that takes as input a large message and produces as an output a small, fixed length message associated to your message m. This is called message digest and we identify the function by H(m). There are two widely used hash functions:
  • MD5 hash function widely used (RFC 1321) computes 128-bit message digest in 4-step process.
  • SHA- 1 is also used as US standard (160-bit message digest). Hash functions are applied to reduce the computational effort of digital signatures. Hash is a non-invertible function that maps a string of arbitrary length to a string of predefined length. Certification Authority In order to verify the digital signature, we must be sure we are using the correct public key, that is we need to be sure to the identity associated with the public key. The idea is to resort to public-key certification. Certification authority (CA) binds public key to particular entity. The authority makes the checking, and then

11 provides a digital certificate stating that this public key is associated with some identity. Every time we want to verify a certificate, you don’t need to contact the authority. The authority basically signs the private key of them. There are different checking that the CA can do, and usually the CA will guarantee that the association is correct and if it’s not they will pay you money: this is the security level of the digital certificate. The public key certification prevents situations like ap 5.0. How can we be sure that a third person is not able to forge the public key certification? The idea is to use the session key as much as possible because the direct application of public key cryptography requires a lot of computation, and we want to save computation while preserving strong cryptographic guarantee. Symmetric key presents the same guarantee as public key. So, in this scenario we have three keys:

  1. The private key of Alice used for the digital signature.
  2. A randomly generated symmetric key used to provide the message confidentiality.
  3. The public key of Bob used to securely communicate the value of the symmetric key. Bob is able to apply his private key to recover the value of the symmetric key to decrypt the message. SSL: Secure Sockets Layer There is only one unique standard of security on the internet and is the one used when we access to some websites. It provides confidentiality, integrity, authentication. We have two protocols that are TCP and IP and are the Internet protocol, used for standard communication. The application is the Internet browser exchanging messages among servers. The idea is that the application instead of directly using the TCP/IP uses and intermediate protocol named SSL. SSL provides application programming interface (API) to applications. Security is really a complex issue
  4. Malware (Virus, Spyware, Trojan horses): software whose behaviour is not the intended one but rather it is performing some hidden actions aimed at altering the security of our system.
  5. Botnet (DDOS): denial of service attack, it’s not targeted to achieve the control of a system but instead it prevents the use of a system by legitimate users by overloading the system with too many requests.
  6. Spear-phishing: targeted attacks to specific individuals/companies. When you receive mails that pretend to be sent by someone you know while are instead sent by hackers asking for money or digital identity. Key escrow The term "escrow," as used conventionally, implies that some item of value (money, physical object) is delivered to an independent trusted party that might be a person or an organization (an escrow agent) for safekeeping, and is accompanied by a set of rules provided by the parties involved in the transaction. Such rules typically specify what is to be done with the item, the schedule to be followed, and the list of other events that have to occur. In any event, the salient point is that all terms and conditions and functioning of an escrow process are, or can be, visible to the parties involved. Moreover, the behaviour and performance of formal escrow agents are governed by legally established obligations. Many companies need to protect some documents, but they need to allow multiple people to access them. The message is not individual property of a person but of the company. We want to achieve this target without the need of sharing any key. Key escrow encrypting techniques allow multiple users to access to encrypted message without requiring any key sharing. The idea is: whenever I want to encrypt a message, the system automatically generates a session key, that is a symmetric key, and uses this key to encrypt a message. Then is encrypt the value of the symmetric key with my public key, so in the future using my private key I will be able to recover the value of the symmetric key to decrypt the message. In addition, the session key is also encrypted with the public key of all the people that should be able to access the message. The