Monday, October 7, 2013

Swapping values without using any other variables

Swapping values between 2 variables

   Before swap:
       a=1;
       b=2;

   After swap:
       a=2;
       b=1;


0. Usual way: Using a temporary variable to store value of one variable before replacing its value.
       temp=a;
       a=b;
       b=temp;


Without using any other variables

1. Using XOR or Exclusive Or (^)
       a=a^b;
       b=b^a;
       a=a^b;
   
    Can also be written as:
       a^=b;
       b^=a;
       a^=b;

       Note: Best method (so far) as it uses only the amount of bits that it has. No worry of overflow.

2. Using Addition (+) and Subtraction (-)
       a=a+b;
       b=a-b;
       a=a-b;
   
       Can also be written as:
       a+=b;
       b=a-b;
       a-=b;
   
       Note: Possibility of overflow if any of the value is near maximum.


3. Using Multiplication (*) and Division (/)
       a=a*b;
       b=a/b;
       a=a/b;

       Can also be written as:
       a*=b;
       b=a/b;
       a/=b;

       Note:
           1. Will not work if any of the value is 0 (zero). 
           2. Possibility of overflow if any of the value is near maximum.

You can try them all online here: http://repl.it/languages/JavaScript

Tuesday, September 24, 2013

Encypt & compress files and send to Dropbox folder

Using the below details you can encypt and compress files and add to Dropbox folder using right-click "Send To" menu.

This is very handy when transferring sensitive / confidential information over cloud storage like Dropbox. Even though most of the cloud storage applications claim to store data in encrypted format, its always better to have your own added security. You never know if that data gets hacked or seen by the cloud storage employees.

I initially searched the net for a solution, which I thought would already exist as this requirement of mine seemed quite basic. Unfortunately, I did not come across any simple and elegant solutions.

What I required was this:
Right-click on a file/folder in Windows OS and click on 'Send to Drobox folder' on your PC. This should encrypt the file and then create a copy of the encrypted file in the Dropbox folder on the PC which would then sync up to the cloud storage (Dropbox).

What I tried first ... in vain:
My initial thought was to create an encrypted 7-zip archive file and then keep dumping files/folders into it using drag-drop. But, when I tried it, I learnt something more about 7-zip. Once a 7-zip archive file is encrypted you cannot use 7-zip to add more files into this archive with encryption. If you add files, they remain unencrypted. Maybe, 7-zip can add this as a feature to prompt for a password when adding files and folders to such encrypted archives and if the password provided matches the archive's encryption password, then encrypt and add files to the archive. I found this as one of the requests at the 7-zip forum.

My solution (works great):
I used windows batch commands (*.bat files) along with free open source 7-zip command line utility (for AES-256 encryption & 7z compression) to encrypt and compress files/folders and send to Dropbox local folder on my PC.

Steps:


Download and install the following software as per the information provided below.

1. Dropbox - Cloud storage application. You should also create/have a login. Dropbox provides 2 GB free storage to start with and with referrals and other mechanisms provides an opportunity to get around 16-20 GB of free storage. [Download Dropbox Windows application]
Assuming Dropbox install path to be: D:\Dropbox
Create local folder for sending encrypted files to: D:\Dropbox\common

2. 7-Zip - Free open source archiver which also provides strong AES-256 bit encryption. [Download 7za.exe standalone command line archiver]
Assuming 7za path to be: D:\Dropbox\7zip  (Note: You should just unzip the 7za920.zip file downloaded from my above link into this folder. Tip: If you share this 7zip folder over dropbox, you do not require to install 7za.exe on all your other computers. Dropbox will sync it automatically.)
3. Batch file to "Send to Dropbox common folder"
Copy below file to "D:\Private" folder (Note: Ensure that this folder is only on your PCs and not on cloud. This folder and more importantly the batch files should not be stored on cloud in an unencrypted format as it contains your password for encryption and decryption).
Filename: send_to_dropbox.bat

@ECHO OFF
REM send_to_dropbox.bat
REM Using 7-zip in commANd line mode to encrypt and send fILes and folders to dropbox

REM Set path to 7za.exe
SET zpath="D:\Dropbox\7zip\7za.exe"

REM Set path to Dropbox folder
SET dbpath="D:\Dropbox\common"

REM Set default password for encryption
SET keyphrase=
Enter_Your_Password_Here_Using_Alphabets_And_Digits_Only

REM ECHO %*

REM Parse input parameters
FOR %%A IN (%*) DO (
    REM echo item=%%A
   
    REM Use 7-zip
    REM a = Archive
    REM -t7z = SettiNg Type of archive to 7z
    REM -r = Recurse subdirectorIes
    REM -mx1 = Setting compression Method to 1 (fastest)
    REM -p{keyphrase} = Setting keyphrase
    REM -mhe=on = Enables archive header encryption
    %zpath% a -t7z -r -mx1 -p%keyphrase% -mhe=on %%A.7z %%A
   
    REM Move the *.7z fiLe to Dropbox folder
    MOVE %%A.7z %dbpath%
)

REM ECHO All done

REM PAUSE

4. Batch file to "Open from Dropbox common folder"
Copy below file to "D:\Private" folder (Note: This folder is only on your PCs and not on cloud. This folder and more importantly the batch files should not be stored on cloud in an unencrypted format as it contains your password for encryption and decryption).
Filename: open_from_dropbox.bat
@ECHO OFF
REM open_from_dropbox.bat
REM Using 7-zip in commANd line mode to decrypt and open fILes and folders from dropbox

REM Set path to 7za.exe
SET zpath="D:\Dropbox\7zip\7za.exe"

REM Set path to Dropbox folder
SET dbpath="D:\Dropbox\common"

REM Set path to output folder
SET outpath="C:\temp\dropboxout"

REM Set default password for decryption
SET keyphrase=Enter_Your_Password_Here_Using_Alphabets_And_Digits_Only

REM Create output folder. Will not overwrite if already exists.
mkdir %outpath% 2>nul

REM ECHO %*

REM Parse input parameters
FOR %%A IN (%*) DO (
    REM echo item=%%A
   
    REM Use 7-zip
    REM x = eXtract usiNg path
    REM -r = Recurse subdirectorIes
    REM -p{keyphrase} = Setting keyphrase
    REM -o{outpath} = Output directory
    %zpath% x -r -p%keyphrase% %%A -o%outpath%
)

REM Open output foLder
START "title" %outpath%

REM ECHO All done

REM PAUSE

5. Send To menu
  • Create shortcuts to each of the *.bat files by right-clicking on the file and using "Send To" > "Desktop (create shortcut)"
  • Rename the shortcuts to "Send to Dropbox" and "Open from Dropbox" respectively.
  • Change the icons of the batch file shortcuts as per your preference to indicate "Send to Dropbox" and "Open from Dropbox". You can search download *.ico files using Google).
  • Right-click on Windows Start button Windows Start Button
  • Click on "Explore" which should open up "Start Menu" in Explorer.
  • On the left side, in the Folders section, you should be able to see "SendTo" folder just above "Start Menu". Left-click on "SendTo" to open it in the right side.
  • Move the batch file shortcuts to "SendTo" folder and close the explorer window.
  • You will now be able to righ-click on any file, folder or multiple files/folders and then see the 2 options "Send to Dropbox" and "Open from Dropbox" in the "Send To" menu.
  • Click on "Send to Dropbox" after right-clicking the file to send a file to "D:\Dropbox\common" folder. The file will get compressed to 7z format and also get encrypted. This encrypted and compressed file will get copied to "D:\Dropbox\common" folder.
  • Follow all the above steps from another PC (possibly at work/home), and share the "D:\Dropbox\common" folder across the 2 (or more) PCs
  • Then on that computer, click on "Open from Dropbox" after right-clicking the *.7z file from "D:\Dropbox\common" folder and viola! ... the file gets uncompressed and decrypted and stored in "C:\temp\dropboxout" folder on your PC.
  • You can then open it / move it to any other folder. You can then also delete the *.7z file as required.
Folders used:
  1. D:\Dropbox
  2. D:\Dropbox\common
  3. D:\Dropbox\7zip
  4. %AppData%\Microsoft\Windows\SendTo
  5. C:\temp\dropboxout
Software used:
  1. Dropbox - [Download Dropbox Windows application
  2. 7-Zip - [Download 7za.exe standalone command line archiver]
  3. Windows Operating System
Why compression? or Why not just stick with encryption alone?
I have used compression (in the fastest mode) as I wanted to reduce the file-size wherever possible so that cloud synchronization would be faster. Also, I didn't want 7-zip to spend a lot of time compressing the files. You can use -mx0 if you want no compression and only storage along with encryption. Alternatively, you can use higher compression switches (-mx3 or -mx5) for better compression / smaller files to be uploaded... but, remember that it does impact speed. I had done some sample testing and then decided on -mx1 to be best suited for me. It is fast and gives me good compression. The switches -mx3, 5, 7 & 9 took more time and there was not a very huge difference in the compressed file size. But, YMMV.

Let me know your comments & suggestions on this topic. At the moment, I am quite happy with my solution. If you share any tips with me to improve this topic, I will try to incorporate it here.

PS 1: I came across "http://satyadeepk.in/dropbox-folder-sync/" when writing this blog. This ready-to-use software may be useful to  people reading this blog.

PS 2: After writing this post, I came across Boxcryptor which supports variety of cloud storage solutions.

PS 3: LifeHacker.com published my solution  :-)   [Link]

Saturday, August 11, 2012

Einstien's Relativity & Speed of Light


E = m c

The most famous equation which gives the relationship between mass and energy. Mass can be be converted to energy (and vice versa). Refer this link for more details on this.

It also dictates that mass of an object will increase as the speed increases and even more so when approaching the speed of light.

Almost everyone thinks that this puts a damper on achieving the speed of light, since as we approach the speed of light the mass increases considerably. So, we need more energy to propel the object due to its increased mass. As the speed increases, the mass increases and so does the need for energy required to propel the object.

But, how about this: if we use the first equation and convert the gained mass back to energy and use that to propel the object instead. Viola! We have solved both problems with this solution.

Now all one has to do is create an object which can convert its mass to energy and use this energy to propel itself. The more mass it has, the more energy it should generate.

Could this be why particles moving at speed of light (or faster) have zero or negligible mass? Are these particles converting their gained mass to energy which keeps propelling them at such high speeds?


Thursday, July 26, 2012

Infinities!

"There are more real numbers between 0 and 1 than all the integers"

Ever since I came across the statement that the set of real numbers between 0 and 1 (eg: 0, 0.1, 0.2) is larger than the set of integers (0, 1, 2), I was curious to understand why it is so.

To understand this, I was reading about "Cantor's Diagonal Argument" here and here.

In very simple terms, these articles provide the below explanation.

Every integer is tried to be mapped to every real number with one-to-one correspondence.
Eg:
   1 --> 0.1
   2 --> 0.2
   3 --> 0.3
   and so on... till infinity.

The above gives an impression that we should always be able to easily map an integer to a real number as the left-side (real numbers) is simply right-side multiplied by 10. In other words, multiply real number by 10 and you should get the integer number which it maps to. For 0.11, multiply by 100 and so on. You get one-to-one correspondence. Then how can there be more real numbers between 0 and 1 as described in the first statement?!

This one-to-one correspondence is only true for:
1. real numbers which are not having repeating decimal (eg. 0.1111..., 0.123123123...) or
2. if they are not irrational numbers (eg. pi=3.1415..., e, square root of 2, 0.1234...)
And why is that? It is because of the definition of "Integer", which states that it has to be a complete number. It cannot be incomplete and hence, has to be well-defined. But, real numbers do not have this limitation and they can have an infinite representation even in a single real number. Eg: Real number 0.333... has infinite 3s after the decimal point. (But, it can be also be easily represented as a fraction: 1/3). Similarly, we have pi, which is an irrational number (which cannot be represented by fraction of whole numbers).

So, with our earlier logic of multiplying an integer by 10, we will not be able to map an integer to the real number represented by 0.333... as there is no integer called 333... We have 333 and 3333 and 33333, and this can continue till infinity. But, all these numbers would always be complete in themselves as per the definition of "integer".

What this means is that, we cannot have an integer to represent irrational numbers, repeating decimals and real numbers which having a representation which in itself is infinite (eg: 0.1234...). This breaks the one-to-one correspondence and shows that there should be more real numbers between 0 and 1 than all the integers.

Also, the multiplication logic for mapping may not work to begin with as we will not know what power of 10 to use to multiply as a single real number's representation can be infinite (Eg: 0.123456789101112..., 0.111...).

There can be infinite such real numbers which cannot be mapped to integers. The integers would get exhausted before being mapped to these real numbers.

Hence, "There are more real numbers between 0 and 1 than all the integers".

The same can be extrapolated for:
"There are more real numbers between 0.1 and 0.2 than all the integers"
"There are more real numbers between 0.01 and 0.02 than all the integers"
"There are more real numbers between 0.001 and 0.002 than all the integers"
... and so on.

So, the infinity in integers isn't very big at all. In fact, it is stated that the smallest ordinal infinity is that of the (postive/negative) integers, and any set which has the cardinality of the integers is countably infinite. Read more about Infinity.


Additional points to ponder upon:

Pi
If pi (3.1415..) is irrational, then we can never have a circle which has both diameter and circumference) as a rational number. This is because: pi is defined as ratio of Circumference of a circle to its diameter.
If C is circumference of the circle and d is diameter. Then, pi = C/d.
Now, since pi is proved to be irrational, by definition, it cannot be represented as a fraction of integers. Hence, either C or d has to be irrational.


Logarithm

If, an = b  -->  a to the power n = b

Then, loga b = n  -->  log b to the base a = n
   Now, if a=1
then, 1n = 1, for all n
=> b=1

Hence, log1 1 = Any value of n. Hence, undefined.

Conclusion: Logarithm to the base 1 is undefined!

Wednesday, July 25, 2012

Windows recursive copy and cksum


Option 1  [Recommended – as it gives output in a consistent format of cksum, filesize, filename without path]
USING rcopy to copy all files into a single directory and then cksum
  1. Open notepad and copy-paste the below rcopy part into it.
  2. Save file as “rcopy_cmd.cmd”
  3. To copy all files to a single directory, execute the following command:  rcopy_cmd.cmd [SVN_LATEST_FILE_PATH_ON_LOCAL_MACHINE]
  4. Finally, run the command from “D:\TESTCOPY” directory: [CKSUM.EXE_PATH]\cksum.exe * > cksum_off.txt
  5. Zip the cksum_off.txt and email it.

       Note:  
         cksum.exe can be from http://gnuwin32.sourceforge.net/packages/coreutils.htm
         Do not forget the dependencies (libintl3.dll, libiconv2.dll) available on same site.


      Standalone recursive copy command execution

rcopy
==START==
@REM  Batch file to recursively get copy files to "D:\TESTCOPY" in Windows
@REM  Usage: rcopy_cmd.cmd [DIR]


@echo off

echo START: %date% %time%

@REM  Go to input DIR
pushd "%1"

@REM  For every file in current dir do (...) [/r = Recursively for every directory]
for /r %%f in (*) do (
  copy "%%f" "D:\TESTCOPY"
  )

@REM  Get back to present working directory
popd

echo END: %date% %time%

@echo on
==END==



cksum.exe "D:\TESTCOPY" > cksum_out.txt


Option 2

      Standalone recursive cksum command execution

cksum_cmd
==START==
@REM  Batch file to recursively get cksum in Windows
@REM  Usage: cksum_cmd.cmd [DIR] > cksum_out.txt


@echo off

echo START: %date% %time%

@REM  Go to input DIR
pushd "%1"

@REM  For every file in current dir do (...) [/r = Recursively for every directory]
for /r %%f in (*) do (
  D:\work\bin\cksum.exe "%%f"
  )

@REM  Get back to present working directory
popd

echo END: %date% %time%

@echo on
==END==


EXTRA

Process to copy all files stated in one list file to one directory.

@REM Create the directory to which the files are to be copied to
mkdir D:\TESTCPY2

@REM Assuming all files are present in one directory called “All_Files”
@REM Read one line at a time from the list file
for /F "tokens=*" %L in (list_of_files.txt) do (
  copy All_Files\%L D:\TESTCPY2
)

Note:  To use the above command in a batch file, change %L to %%L
 

Friday, May 18, 2012

Free Will versus Destiny


DESTINY

Whatever has happened, whatever is happening and whatever will happen has already been decided. So, whatever one does or does not do is no longer a choice and hence, it is destiny.

Let’s say that the past, present and future information is written by a creator in a book called Predictions. But this book can never be accessed by anyone other the creator. Neither is the creator accessible to anyone.

In this case, there is a book which has all actions written. But, since the book is not accessible, no one other than the book’s creator knows about it. Hence, whatever one does, even thought already predicted, is not known and hence, it is free will.

FREE WILL

At any point in time one can choose what to do or what not to do. This is completely up to the person and the circumstances at the time of action or inaction. Hence, it is free will.

Let’s say that all that one has done and one is doing is being written in a book called Predictions by a creator. But, neither the creator not the book is accessible to anyone. The creator also writes future actions and inaction into this book as this book has no bearing on anyone as it is anyways inaccessible.

As the book is always inaccessible, one can say that whatever one does or does not do is already predicted in the book. Hence, it is destiny.

 Is free will the same as destiny?!

Thursday, February 23, 2012

Convex Universe

What if the universe is convex on a grand scale?

Would it explain the acceleration of galaxies moving away from each other?

On a small scale, the universe would behave like a normal cube, so any matter would bend the space to some extent causing gravity. But, on a large scale, the convex universe would be pushing things away from each other and the farther you go, the more convex it would be.

A simpler example to understand this would be to consider the planet Earth. When we consider a small area, say 10 square meters, we perceive it to be flat and, in earlier times, that could have been one of the main reasons to believe that the Earth was flat. But, with some techniques and better understanding we now believe that the Earth is spherical (geoid) in shape. When we look at it from a larger perspective (not just the few square meters), we perceive the Earth as a sphere.

Similarly, when we look at the universe on a smaller scale (say, few thousands of light years), we perceive it to be flat... but, it may well be bent as a convex.

Now, back to pondering whether the universe is finite in space (having a boundary) or is infinite (unbound).