<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Orkhan Huseynli</title>
    <description>The latest articles on DEV Community by Orkhan Huseynli (@orkhanhuseyn).</description>
    <link>https://dev.to/orkhanhuseyn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F217584%2F71dd3302-1cb1-45a2-bfa2-0828d339e883.jpeg</url>
      <title>DEV Community: Orkhan Huseynli</title>
      <link>https://dev.to/orkhanhuseyn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/orkhanhuseyn"/>
    <language>en</language>
    <item>
      <title>A gentle introduction to signed integer representation</title>
      <dc:creator>Orkhan Huseynli</dc:creator>
      <pubDate>Mon, 09 Sep 2024 05:20:09 +0000</pubDate>
      <link>https://dev.to/orkhanhuseyn/a-gentle-introduction-to-signed-integer-representation-5e87</link>
      <guid>https://dev.to/orkhanhuseyn/a-gentle-introduction-to-signed-integer-representation-5e87</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When we talk about numbers and data types in programming we generally mention how many bytes they occupy in memory. For example, we say that 32 bit integer can hold values from 0 up to 2&lt;sup&gt;32&lt;/sup&gt;-1, 64 bit can hold up to 2&lt;sup&gt;64&lt;/sup&gt; - 1 and etc. However, this examples are true when the integer is unsigned, which means that it consists of only positive numbers.&lt;/p&gt;

&lt;p&gt;In signed integers, the ones that hold both negative and positive values, the minimum and maximum values are different: 32 bit signed integers can hold values from -2&lt;sup&gt;31&lt;/sup&gt; up to 2&lt;sup&gt;31&lt;/sup&gt;-1. &lt;/p&gt;

&lt;p&gt;Why? To understand it deeply, let see it visually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unsigned integers in binary
&lt;/h2&gt;

&lt;p&gt;Let's say we have a 4 bits and we want to store unsigned integers in this 4 bits. The minimum value that we can store is 0 when the maximum value is 15. Let's write them all down to see it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0000 -- 0 in decimal, this is the minimum
0001 -- 1 in decimal
0010 -- 2 in decimal
0011 -- 3 in decimal
0100 -- 4 in decimal
0101 -- 5 in decimal
0110 -- 6 in decimal
0111 -- 7 in decimal
1000 -- 8 in decimal
1001 -- 9 in decimal
1010 -- 10 in decimal
1011 -- 11 in decimal
1100 -- 12 in decimal
1101 -- 13 in decimal
1110 -- 14 in decimal
1111 -- 15 in decimal, this is the maximum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is pretty clear and an usual way of how we think of binary representation of integers. What about negative numbers? If 4 bit integer was signed then its minimum value would be -8 (-2&lt;sup&gt;3&lt;/sup&gt;) when the maximum value would be 7(2&lt;sup&gt;3&lt;/sup&gt;-1). &lt;/p&gt;

&lt;p&gt;Again why? Let's write them down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0000 -- 0 in decimal
0001 -- 1 in decimal
0010 -- 2 in decimal
0011 -- 3 in decimal
0100 -- 4 in decimal
0101 -- 5 in decimal
0110 -- 6 in decimal
0111 -- 7 in decimal, this is the maximum
1000 -- (-8) in decimal, this is the mimimum
1001 -- (-7) in decimal
1010 -- (-6) in decimal
1011 -- (-5) in decimal
1100 -- (-4) in decimal
1101 -- (-3) in decimal
1110 -- (-2) in decimal
1111 -- (-1) in decimal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, this looks quite confusing, isn't it? One question you may ask is: &lt;em&gt;Why is 1111 is -1 and 1000 is -8?&lt;/em&gt;. Now, if you add -7 and 7 or -1 and 1 or -5 and 5 in binary, you will see that you get 0 in binary (in 4 bytes, ignore the carry). Why this works? Is it a magic? To understand it clearly we need to understand complements technique. The reason, why negative 1 is 1111, because 1111 is 2's complement of positive one (0001). So, what is complement technique?&lt;/p&gt;

&lt;h2&gt;
  
  
  Complement tehcnique
&lt;/h2&gt;

&lt;p&gt;Let's first begin with the decimal world. If we want to find 9's complement of any decimal number, we subtract it from all 9's. For example, if we want to find 9's complement of 58, we subtract 58 from 99:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;99 - 58 = 41 --&amp;gt; is 9's complement of 58&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we add 1 to 41, we will get 42 and that will be 10's complement. So, adding 1 to the 9's complement gave us 10's complement. This is simply, subtracting 58 from 10.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;99 - 58 + 1 = 42 --&amp;gt; is 10's complement of 58&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Similarly, if we want to find 2's complement of any binary number, we subtract it from all 1's to find 1's complement and add 1 to it to get 2's complement. So, 1's complement of 1001 is: &lt;code&gt;1111 - 1001 = 0110&lt;/code&gt; and adding 1 to it will give us 2' complement: &lt;code&gt;0110 + 0001 = 0111&lt;/code&gt;. But, how computers subtract a number from the other?&lt;/p&gt;

&lt;p&gt;If you were careful, you would notice that to find 1's complement of binary number we don't need to practically subtract the number from 1111. Just flipping the bits would give us the right answer, so instead of subtracting, we could apply logical NOT operator to the number: &lt;code&gt;NOT 1001 = 0110 --&amp;gt; 1's complement of 1001&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Subtracting using complement
&lt;/h2&gt;

&lt;p&gt;Since there is no subtraction operation in computers, we also need to subtract numbers using addition. Luckily, complement technique also helps here.&lt;/p&gt;

&lt;p&gt;Again in the decimal world! Let's say we want to subtract 58 from 87. 10's complement of 58 is 42, and if you add 87 and 42 together it will give you 129. Remove, 1 from 129 (because we are only working with 2 digits and ignoring the overflows) and you will get 29. Guess what? 87 - 58 is 29.&lt;/p&gt;

&lt;p&gt;Similarly, in binary world, if you want to subtract a number from another, find 2's complement of the small number and add it with the other. For example, to calculate &lt;code&gt;1100 1011 - 1001 1100&lt;/code&gt;, (203 - 156 in unsigned representation) find 2's complement of &lt;code&gt;1001 1100&lt;/code&gt; which is &lt;code&gt;NOT 1001 1100 + 0000 0001 = 0110 0011 + 0000 0001 = 0110 0100&lt;/code&gt; and then adding it with the first number will give us &lt;code&gt;1100 1011 + 0110 0100 = 010010 1111&lt;/code&gt;. Since we ignore overflows, we will ignore first two digits of the result and our result will be &lt;code&gt;0010 1111&lt;/code&gt; which is 47 in decimal (203 - 156 = 47).&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now, hopefully it makes sense why the negative numbers were depicted in the way they are. And as you already understood from the examples above any signed integers minimum value will be -2&lt;sup&gt;n-1&lt;/sup&gt; and maximum value will be 2&lt;sup&gt;n-1&lt;/sup&gt;-1. If anyone asks you what is, let's say negative 8 in binary, just find what is positive 8 in binary, then two's complement of that number will be negative 8.&lt;/p&gt;

&lt;p&gt;The contents of this article is heavily based on &lt;a href="https://www.udemy.com/course/number-system/" rel="noopener noreferrer"&gt;Number Theory for Programmers&lt;/a&gt; course on Udemy, so I highly recommend you to enroll and learn more.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Making Your NPM Package Executable</title>
      <dc:creator>Orkhan Huseynli</dc:creator>
      <pubDate>Wed, 12 Apr 2023 21:13:45 +0000</pubDate>
      <link>https://dev.to/orkhanhuseyn/making-your-npm-package-executable-1j0b</link>
      <guid>https://dev.to/orkhanhuseyn/making-your-npm-package-executable-1j0b</guid>
      <description>&lt;p&gt;You've probably seen such NPM packages, that you can execute on the shell directly. Let's take &lt;a href="https://www.npmjs.com/package/cowsay" rel="noopener noreferrer"&gt;cowsay&lt;/a&gt; package for example. If you run &lt;code&gt;npx cowsay Mooo!&lt;/code&gt; in your terminal, you'll see such an output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffe378j026nopmdyv0ciz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffe378j026nopmdyv0ciz.png" alt="output of running cowsay on terminal which shows ascii cow saying Mooo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can install this package as a global dependency and then run it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; cowsay
&lt;span class="nv"&gt;$ &lt;/span&gt;cowsay Mooo!


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Shebang
&lt;/h1&gt;

&lt;p&gt;The reason why we can run it in the terminal directly using &lt;code&gt;cowsay&lt;/code&gt; command is because its executable file added to our &lt;code&gt;PATH&lt;/code&gt; when we installed it with &lt;code&gt;-g&lt;/code&gt; flag. You can see where it's located using &lt;code&gt;which&lt;/code&gt; command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;which cowsay
/usr/bin/cowsay


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you inspect it with long listing format, you'll notice that, it's actually a symbolic link to a JavaScript file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; /usr/bin/cowsay
/usr/bin/cowsay -&amp;gt; ../lib/node_modules/cowsay/cli.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When we install a package globally on a Linux system, the package will be downloaded into &lt;code&gt;{prefix}/lib/node_modules&lt;/code&gt; folder and its &lt;code&gt;bin&lt;/code&gt; files will be linked to &lt;code&gt;{prefix}/bin&lt;/code&gt; folder. In our case the &lt;code&gt;prefix&lt;/code&gt; is just &lt;code&gt;/usr&lt;/code&gt; (run &lt;code&gt;npm prefix -g&lt;/code&gt; to see it).&lt;/p&gt;

&lt;p&gt;But how does the bash know which interpreter to use to execute this file? Well, if you see the first line of this JavaScript file you'll notice an output as following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt; /usr/lib/node_modules/cowsay/cli.js
&lt;span class="c"&gt;#!/usr/bin/env node&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This line is called &lt;a href="https://en.wikipedia.org/wiki/Shebang_(Unix)" rel="noopener noreferrer"&gt;hash bang&lt;/a&gt; (or shebang) and it is used in scripts to indicate an interpreter for execution under UNIX/Linux operating systems. Simply, this is a directive for the bash that tells it to use indicated interpreter to execute the file.&lt;/p&gt;

&lt;p&gt;Let's create a JavaScript file which has no extension and try to execute it using the shell:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;my_script
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"const os = require('os'); console.log(os.cpus().length);"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; my_script
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;u+x my_script
&lt;span class="nv"&gt;$ &lt;/span&gt;./my_script
./my_script: line 1: syntax error near unexpected token &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'
./my_script: line 1: `const os = require('&lt;/span&gt;os&lt;span class="s1"&gt;'); console.log(os.cpus().length);'&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you see these errors, then you're on the right track. The file contains JavaScript code but the shell by default will try to interpret it using &lt;code&gt;/usr/bin/bash&lt;/code&gt; interpreter, if you don't believe me, just run &lt;code&gt;bash my_script&lt;/code&gt; to get the same results.&lt;/p&gt;

&lt;p&gt;To execute this file using Nodejs, we either need to run &lt;code&gt;node my_script&lt;/code&gt; or we need to add Shebang line. Let's do the latter. After adding the Shebang, your &lt;code&gt;my_script&lt;/code&gt; file should look like mine:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cp"&gt;

#!/usr/bin/node
&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;os&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;os&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cpus&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can try running it again as before or you can move this file to &lt;code&gt;/usr/bin&lt;/code&gt; directory and try running it directly on your terminal. It will show you the number of logical CPU cores on your machine. For me it is 16:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo mv &lt;/span&gt;my_script /usr/bin/my_script
&lt;span class="nv"&gt;$ &lt;/span&gt;my_script
16


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Hi-Mom CLI
&lt;/h1&gt;

&lt;p&gt;Now, let's turn famous &lt;code&gt;hi-mom&lt;/code&gt; NPM package to a CLI application. Firsly, let's clone the repository and open it in our editor:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;git clone https://github.com/tsivinsky/hi-mom.git
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;hi-mom &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; code &lt;span class="nb"&gt;.&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's create a &lt;code&gt;cli.js&lt;/code&gt; file in the root of the project folder and add the following code as its contents:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cp"&gt;

#! /usr/bin/node
&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;hiMom&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lang&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hiMom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lang&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Also add the following line to the &lt;code&gt;package.json&lt;/code&gt; file to show NPM where to look for &lt;code&gt;bin&lt;/code&gt; files:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"bin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./cli.js"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We also need to make sure that this file has execute permission for all users and groups:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x cli.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, if you run &lt;code&gt;npx hi-mom&lt;/code&gt; you'll see the desired output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxsx1m7gks15l96oi58i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxsx1m7gks15l96oi58i.png" alt="result of calling npx hi-mom on the terminal which says hi mom"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To be able to call it whithout npx, we need to install it globally, hence this package needs to be published to NPM registry. But to keep it simple, we can just create a symbolic link for it. Just run &lt;code&gt;npm link&lt;/code&gt; in the root of project folder.&lt;/p&gt;

&lt;p&gt;Then you can run it everywhere:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbwa0or792xqmngus3pgg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbwa0or792xqmngus3pgg.png" alt="running hi-mom after using npm link on terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I hope this article was helpful. If you have any questions or contradictions, please leave a comment and if you liked this post, like and share. Thanks a lot for reading.&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://docs.npmjs.com/cli/v9/configuring-npm/package-json#bin" rel="noopener noreferrer"&gt;https://docs.npmjs.com/cli/v9/configuring-npm/package-json#bin&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.npmjs.com/cli/v9/commands/npm-install#global" rel="noopener noreferrer"&gt;https://docs.npmjs.com/cli/v9/commands/npm-install#global&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.npmjs.com/cli/v9/configuring-npm/folders#description" rel="noopener noreferrer"&gt;https://docs.npmjs.com/cli/v9/configuring-npm/folders#description&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.baeldung.com/linux/shebang" rel="noopener noreferrer"&gt;https://www.baeldung.com/linux/shebang&lt;/a&gt;&lt;/p&gt;

</description>
      <category>npm</category>
      <category>node</category>
      <category>bash</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Verifying Integrity of Files using NodeJS</title>
      <dc:creator>Orkhan Huseynli</dc:creator>
      <pubDate>Sat, 01 Apr 2023 19:22:14 +0000</pubDate>
      <link>https://dev.to/orkhanhuseyn/verifying-integrity-of-files-using-nodejs-1gnd</link>
      <guid>https://dev.to/orkhanhuseyn/verifying-integrity-of-files-using-nodejs-1gnd</guid>
      <description>&lt;p&gt;While using third party JavaScript or CSS libraries from CDN, you've probably came across with &lt;code&gt;integrity&lt;/code&gt; attribute on &lt;code&gt;script&lt;/code&gt; or &lt;code&gt;link&lt;/code&gt; tags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script 
  &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://code.jquery.com/jquery-3.6.4.min.js"&lt;/span&gt; 
  &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8="&lt;/span&gt; 
  &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you haven't researched about them till now, then you're in the right place.&lt;/p&gt;

&lt;h1&gt;
  
  
  Man in the Middle Attacks
&lt;/h1&gt;

&lt;p&gt;It's possible that the any data which travels through internet can be modified till it reaches to our machine. An attacker might use techniques such as &lt;a href="https://en.wikipedia.org/wiki/Network_eavesdropping" rel="noopener noreferrer"&gt;network eavesdropping&lt;/a&gt; to intercept the requests and responses between you and the server and might manipulate the file before you even receive it.&lt;/p&gt;

&lt;p&gt;We need a way to verify that the file we've received has not been tempered with.&lt;/p&gt;

&lt;h1&gt;
  
  
  Subresource Integrity in Browsers
&lt;/h1&gt;

&lt;p&gt;Browsers implement a security feature called &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity" rel="noopener noreferrer"&gt;Subresource Integrity&lt;/a&gt; (SRI) to verify integrity of the resource they fetch and execute.&lt;/p&gt;

&lt;p&gt;The garbage looking string of characters you see as the value of &lt;code&gt;integrity&lt;/code&gt; attribute is Base64 decoded cryptographic digest formed by applying specific &lt;a href="https://en.wikipedia.org/wiki/Cryptographic_hash_function" rel="noopener noreferrer"&gt;hash algorithm&lt;/a&gt; to the contents of the file.&lt;/p&gt;

&lt;p&gt;If any characters inside the file changes, then the calculated hash will also change, thus integrity verification will fail, thus the browser will know that the file has been tempered with. &lt;/p&gt;

&lt;p&gt;If browsers cannot verify integrity of the file being requested, they'll show error message similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Failed to find a valid digest in the 'integrity' attribute 
for resource '&amp;lt;resource-name&amp;gt;' with computed SHA-256 integrity '&amp;lt;calculated-hash&amp;gt;'. 
The resource has been blocked.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Performing Integrity Check with NodeJS
&lt;/h1&gt;

&lt;p&gt;To show a little demo how this check might be implemented, I'll use &lt;a href="https://nodejs.org/api/crypto.html" rel="noopener noreferrer"&gt;NodeJS's &lt;code&gt;crypto&lt;/code&gt; module&lt;/a&gt;. We'll use the same &lt;code&gt;jquery&lt;/code&gt; file the you saw in the first paragraph. I'll download that file into my machine to be able to make modifications later.&lt;/p&gt;

&lt;p&gt;Let's create an &lt;code&gt;index.js&lt;/code&gt; file and import &lt;code&gt;fs&lt;/code&gt; and &lt;code&gt;crypto&lt;/code&gt; modules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;crypto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll need &lt;code&gt;fs&lt;/code&gt; module to read contents of the file that we want to verify integrity of. Let's declare resource name and expected integrity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jquery-3.6.4.min.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expectedIntegrity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;sha256&lt;/code&gt; in front of the value of &lt;code&gt;integrity&lt;/code&gt; attribute is just the name of hashing algorithm that this hash has been calculated with, so we skip it when comparing.&lt;/p&gt;

&lt;p&gt;Let's read the file contents and calculate the digest using &lt;code&gt;sha256&lt;/code&gt; algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jquerySource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;digest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sha256&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jquerySource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;calculatedIntegrity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of &lt;code&gt;digest&lt;/code&gt; variable is a type called &lt;code&gt;Buffer&lt;/code&gt; which is just bunch of bytes, so we need to convert it to a string in &lt;code&gt;base64&lt;/code&gt; encoding.&lt;/p&gt;

&lt;p&gt;And finally, we just need to compare &lt;code&gt;calculatedIntegrity&lt;/code&gt; with &lt;code&gt;expectedIntegrity&lt;/code&gt;, if they are equal then the &lt;code&gt;jquery-3.6.4.min.js&lt;/code&gt; has not been tempered with, if not, it means something has changed inside the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;calculatedIntegrity&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;expectedIntegrity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;✔️ Resource integrity verified!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`❌ Failed to find a valid digest in the 'integrity' attribute 
  for resource '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;' with computed SHA-256 integrity '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;calculatedIntegrity&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;'.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run this code using &lt;code&gt;node index.js&lt;/code&gt; then, you might see this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;node index.js
✔️ Resource integrity verified!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you change the contents of &lt;code&gt;jquery-3.6.4.min.js&lt;/code&gt; file, and run the code again, then you'll see output similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'some malicious code'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; jquery-3.6.4.min.js
&lt;span class="nv"&gt;$ &lt;/span&gt;node index.js
❌ Failed to find a valid digest &lt;span class="k"&gt;in &lt;/span&gt;the &lt;span class="s1"&gt;'integrity'&lt;/span&gt; attribute 
  &lt;span class="k"&gt;for &lt;/span&gt;resource &lt;span class="s1"&gt;'jquery-3.6.4.min.js'&lt;/span&gt; with computed SHA-256 integrity &lt;span class="s1"&gt;'+FiqKbj0h12Db1PZj62G+h2BMtOueBg26YQOJFY+6wg='&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I hope this article was helpful for you, I suggest you read more at &lt;a href="https://www.smashingmagazine.com/2019/04/understanding-subresource-integrity/" rel="noopener noreferrer"&gt;article at Smashing Magazine about Subresource Integrity&lt;/a&gt; and share your thoughts.&lt;/p&gt;

&lt;p&gt;You can see the full version of the code we wrote at &lt;a href="https://gitlab.com/orkhan-huseyn/node-integrity-check" rel="noopener noreferrer"&gt;my GitLab repository&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>cryptography</category>
    </item>
  </channel>
</rss>
