Skip to content

Category: english

heredoc to feed an interactive program

Let this interactive.sh be the interactive program:
[bash]#!/bin/sh
read input1
read input2
echo $input1 $input2
[/bash]

This is a script feed.sh (in the same directoty) that uses heredoc to feed two lines to interactive.sh:
[bash]#!/bin/sh
./interactive.sh << EOF
asdf
zxcv
EOF
[/bash]

It work as:
[bash]$ ./feed.sh
asdf zxcv
[/bash]

“and I know too well that these arguments from probabilities are impostors, and unless great caution is observed in the use of them they are apt to be deceptive-in geometry, and in other things too” Socrates, in Phaedo by Plato. 399 BC.

Jeep Wrangler ASCII Art Collection

Some ASCII/ANSI/Shift_JIS/Unicode art of Jeep Wranglers. They are easy to modify and introduce your own customizations.

These I found on the web:

  __________ 
 |          | 
 |______(*)_| 
 /\__----__/\ 
/_/()||||()\_\ 
|_\ o||||o /_| 
|----Jeep----| 
|_|        |_| 


                   _.------------------.
                 .'____________________|
                 //    _||||  | |  | | |
          ______//_\__j_|||"--" "--" | |  _
         /-----+-|p  ==,|||__________|_|-|W|
        _j,====. |b_____|||  _____     | |W|
       |_) ,---.`.`------'|.',---.`.___|_|W|
         `/ .-. \\`======__// .-. \`-----'""
          \ `-' / """""""   \ `-' /
           `---'             `---'     hjw

  ____        
 /_/CL\___     
 \_,.__,._)    
---`'--`'--MK  
  ____
 |_| L\___
 |_,.__,._)
---`'--`'--MK

/l ,[____],
l---L -O||||||O-
()_)-()_)-o-)_)

    ______ooooo
  /__|_|_,\____\,___
  |_---|_|__|---O|||O
_.(o)_)__(o)_)--o-)_) 

               ∩_∩
        // ̄ヽ_( ・(ェ)・)  ____
       | ̄ ̄|_ (⊃/ ̄ ̄ ̄(O|||||O)
 .⌒ヽ ,;;; | / ̄ヽ ||___|// ̄ヽ ̄| |||||/ヽ
 (";"':;.):;゙ |_| ◎ |___ | ◎ .| |=======
   ⌒;:ヽ;; ヽ_//     ヽ_// ヽ_//
~~~~´゙`゙゙´´ ~~~~゙`゙´``´゙`゙゙´´ ~~゙゙´``´`´ ~~~


,[____l,
_-o||||o-_
()_)¯¯ )_)

O|||||||O

•IIIIII•

This one I created:

 
 â—„
  ╽██████████████       
  ▓█ ██ █✌ຈل͜ຈ █▄▄▄▄▄
  ▓█████████████⚪║║║║⚪█ 
  ═█▓▓██████▓▓▀████ºº███▀
    â–“â–“ â–“â–“   â–“â–“       â–“â–“

This one is a remix of one from hjw (Hayley Jane Wakenshaw) with an extra door, i.e. the Jeep Wrangler Unlimited.


                   _.---------------------.
                 .'_______________________|
                 //    _|||    || |   | | |
          ______//_\__j_|||____|| "---" | |  _
         /-----+-|p  ==,|||p ==,|_______|_|-|W|
        _j,====. |b_____|||b    /_____    | |W|
       |_) ,---.`.`------'|----.',---.`.__|_|W|
         `/ .-. \\`======__====// .-. \`---'""
          \ `-' / """""""  """" \ `-' /
           `---'                 `---'  

A remix of this post from @TwitAA_bot:

  __________ ⌒彡
 |          |冫、)
 |__________|` /
 /\__----__/\ /
/_/()||||()\_\
|_\ o||||o /_|
|------------|
|_|        |_|

Sources:

if host pings execute command

[bash]#!/bin/sh
HOST="silveiraneto.net"
if ping -c 1 $HOST > /dev/null
then
echo your command
fi
[/bash]

  • replace silveiraneto.net for your desired hostname or ip
  • ping sends only one package (-c 1) and ignores the text output
  • a ping is successful returns 0 and the if proceeds
  • replace the echo line with your command

printing only odd or even lines using sed

$ cat AtoF
A
B
C
D
E
F

Print only odd lines

sed -n 'p;n' AtoF
A
C
E

-n suppress automatic printing of pattern space, p print the current pattern space, n read/append the next line of input into the pattern space.

Alternatively:

$ sed -n 1~2p AtoF
A
C
E

-n suppress automatic printing of pattern space, 1~2 from the 1st line match every line every 2 steps, p print the current pattern space. sed -n 0~p has the same effect . print only the even lines.

Print only even lines

$ sed -n 'n;p' AtoF 
B
D
F

or

$ sed -n 2~2p AtoF
B
D
F

0~2p would also work. I prefer

PNG to PDF and PNG to PDF

You can use ImageMagick to convert multiple PNGs into a PDF and convert a multiple pages PDF into multiple PNGs.

PDF to PNG

$ convert document.pdf document.png

For a document with 10 pages, this will generate document-0.png, document-1.png, … document-9.png.

PNG to PDF

$ convert document*.png document.pdf

This convert the PNG pages into pages in the document.pdf document.

One-time pad using GIMP

With a ∈ {0,1} and b ∈ {0,1}, the XOR function {0,1}→{0,1} can be defined by the truth table:

a b a XOR b
0 0 0
0 1 1
1 0 1
1 1 0

Gimp doesn’t have a layer operation called XOR but there is the “Difference” mode defined by the Gimp User Manual by the equation:

$latex E =|I – M|$

Applying the formula to the Red Green Blue color mode for a pixel we have:

$latex rgb(|r_I-r_M|,|g_I-g_M|,|b_I-b_M|) =|rgb(r_I,g_I,b_I) – rgb(r_M,g_M,b_M)|$

The truth table for the “difference” mode is:

I M |I – M|
rgb(0,0,0) rgb(0,0,0) rgb(0,0,0)
rgb(0,0,0) rgb(255,255,255) rgb(255,255,255)
rgb(255,255,255) rgb(0,0,0) rgb(255,255,255)
rgb(255,255,255) rgb(255,255,255) rgb(0,0,0)

Let’s call black-white representation this binary pallet:

  • the white color, i.e. rgb(255,255,255), represent the number 1
  • the black color, i.e. rgb(0,0,0), represent the number 0.

Then the truth table for the difference is the same as the XOR one.

Experimenting with images

Let A_0101.png be this image:
A_0101.png

And B_0011.png be this image:
B_0011

Notice that each image use the black-white representation of the binary message in the file name.

After we apply the “Difference” layer mode with the two images we have what we can call AXORB_0110.png:

A XOR B 0110

One-time pad

Let message.png be this 512×512 image using a 1-bit pallet (Lena with Floyd–Steinberg dithering):

Lena Soderberg

And key.png a image of the same size and pallet with random content:
key

We can create a image messageXORkey.png using the Gimp’s different mode:

messageXORkey

The messageXORkey.png looks as random as the key or any other random key.

Someone in possession of messageXORkey.png and key.png can apply a XOR to retrieve message.png. That’s because:

messageXORkey.png = message.png XOR key.png
messageXORkey.png XOR key.png = message.png XOR key.png XOR key.png
messageXORkey.png XOR key.png = message.png XOR 0
messageXORkey.png XOR key.png = message.png

Considerations

This was a demonstration of how to use GIMP to encode and decode one-time pads. There are several constraints to use one-time pads in a secure way for practical purposes that you should know before using it in a real situation.

The Diffie-Hellman key exchange

Can I have a secret?
Can I tell you a secret?
Can I tell you a secret when we know there is someone snooping around?

I’d like to share this graphical explanation of the Diffie-Hellman key exchange principle without going into the details about the math behind it. It uses physical abstractions as padlocks, keys, and treasure chests. The goal of the key exchange is to allow two parties to establish a shared secret key over an insecure communication channel.

the diffie-hellman key exchange infographic pixelart

Alice and Bob, a complicated couple, would like to talk through an insecure channel.

Step by step

  1. Alice has the padlocks A and C, two keys C and one key A. Bob has the padlock B, key B, and the letter he wants to send to Alice through the insecure channel.
  2. Alice puts the padlock and key C in the chest.
  3. Alice locks the treasure chest using the padlock A, and sends to Bob.
  4. Bob receives the chest. He can’t open it because he doesn’t have the key A. He puts one more lock, the padlock B, in the treasure chest and send it back to Alice. Alice also can’t open the chest, as she doesn’t have the key B.
  5. But Alice does have the key A, which she uses to remove the padlock A from the chest and send it back to Bob.
  6. Bob now receives a chest which he can open. He opens it and receive the padlock and key C. At this point the key exchange is done. He can either keep the chest and padlock C to send something to Alice, or he can use the same key exchange technique from steps 1 to 6 to send the padlock C back to Alice.
  7. Bob decides to send Alice a letter using the padlock C. He puts the letter in the chest.
  8. Bob locks the chest using the padlock C, send it to Alice. He keeps key C.
  9. Alice received the letter and now they both have the key C.

Notice

  • Alice’s key A never left her inventory. Bob’s key B never left his inventory.
  • Neither Alice nor Bob really knows who is in the other side. In this example they just trust in each other. Authentication is very important but is not handled in this example.
  • At every transference a different padlocks (or a combination of padlocks) was used.

eve the diffie-hellman key exchange 2x

A tiny bit of math

Let’s (very) informally define computationally efficient as a computation that someone is willing to wait and pay.

The abstraction here is that a chest with padlock is easy to lock/unlock when you have the correct key but hard to be unlocked otherwise. To use this technique with data, we need a mathematical function f that is:

  • Easy to lock: it is computationally efficient to apply f(m,k) over a message m and a key k
  • Easy to unlock: there is a computationally efficient inverse function f’ such that m = f'(f(m,k),k)
  • Hard to break: it is not computationally efficient to find m or k knowing only f and f(m,k)

If you want to know more about these functions, take a look in the original article “New directions in cryptography” by Diffie, W. and Hellman, M. in 1976.