Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Convert a long to HEX value and the other way around

Handy to convert MD5 or SHA-1 hash values.

	public static long hexToLong(byte[] bytes) {

		if (bytes.length > 16) {
			throw new IllegalArgumentException("Byte array too long (max 16 elements)");
		}
		long v = 0;
		for (int i = 0; i < bytes.length; i += 2) {
			byte b1 = (byte) (bytes[i] & 0xFF);

			b1 -= 48;
			if (b1 > 9) b1 -= 39;

			if (b1 < 0 || b1 > 15) {
				throw new IllegalArgumentException("Illegal hex value: " + bytes[i]);
			}

			b1 <<=4;

			byte b2 = (byte) (bytes[i + 1] & 0xFF);
			b2 -= 48;
			if (b2 > 9) b2 -= 39;

			if (b2 < 0 || b2 > 15) {
				throw new IllegalArgumentException("Illegal hex value: " + bytes[i + 1]);
			}

			v |= (((b1 & 0xF0) | (b2 & 0x0F))) & 0x00000000000000FFL ;

			if (i + 2 < bytes.length) v <<= 8;
		}

		return v;
	}

	public static byte[] longToHex(final long l) {
		long v = l & 0xFFFFFFFFFFFFFFFFL;

		byte[] result = new byte[16];
		Arrays.fill(result, 0, result.length, (byte)0);

		for (int i = 0; i < result.length; i += 2) {
			byte b = (byte) ((v & 0xFF00000000000000L) >> 56);

			byte b2 = (byte) (b & 0x0F);
			byte b1 = (byte) ((b >> 4) & 0x0F);

			if (b1 > 9) b1 += 39;
			b1 += 48;

			if (b2 > 9) b2 += 39;
			b2 += 48;

			result[i] = (byte) (b1 & 0xFF);
			result[i + 1] = (byte) (b2 & 0xFF);

			v <<= 8;
		}

		return result;
	}



And tests:

	public void testHexToLong() throws Exception {
		assertEquals(-7057002501900618110L, NumberUtils.hexToLong("9e107d9d372bb682".getBytes()));
		assertEquals(-10908158098650842L, NumberUtils.hexToLong("ffd93f1687604926".getBytes()));
	}

	public void testLongToHex() throws Exception {
		assertEquals("9e107d9d372bb682", new String(NumberUtils.longToHex(-7057002501900618110L)));
		assertEquals("ffd93f1687604926", new String(NumberUtils.longToHex(-10908158098650842L)));
	}

Basic Rijndael (AES) encryption

using System.IO;
using System.Security.Cryptography;
 
            private static readonly byte[] SALT = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };
 
            public static byte[] Encrypt(byte[] plain, string password)
            {
                MemoryStream memoryStream;
                CryptoStream cryptoStream;
                Rijndael rijndael = Rijndael.Create();
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT);
                rijndael.Key = pdb.GetBytes(32);
                rijndael.IV = pdb.GetBytes(16);
                memoryStream = new MemoryStream();
                cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
                cryptoStream.Write(plain, 0, plain.Length);
                cryptoStream.Close();
                return memoryStream.ToArray();
            }
            public static byte[] Decrypt(byte[] cipher, string password)
            {
                MemoryStream memoryStream;
                CryptoStream cryptoStream;
                Rijndael rijndael = Rijndael.Create();
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT);
                rijndael.Key = pdb.GetBytes(32);
                rijndael.IV = pdb.GetBytes(16);
                memoryStream = new MemoryStream();
                cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
                cryptoStream.Write(cipher, 0, cipher.Length);
                cryptoStream.Close();
                return memoryStream.ToArray();
            }

Local protect data (bind to a machine)

using System.Security.Cryptography;

        private static readonly byte[] salt = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };

        public static byte[] ProtectLocalData(byte[] plain)
        {
            return ProtectedData.Protect(plain, salt, DataProtectionScope.LocalMachine);
        }

        public static byte[] UnprotectLocalData(byte[] cipher)
        {
            return ProtectedData.Unprotect(cipher, salt, DataProtectionScope.LocalMachine);
        } 

Easy file encryption and decryption from the shell

Works on OS X, Linux, anywhere with OpenSSL installed:

To encrypt a file:
openssl des3 -salt -in infile.txt -out encryptedfile.txt


To decrypt the file:
openssl des3 -d -salt -in encryptedfile.txt -out normalfile.txt


Do not specify the same file as input and output on encryption.. I have noticed weird effects on OS X (it eats the file). Remove the -in * stuff if you want to pipe data into it (e.g. a tarred folder). Omit the -out * stuff if you want it to pipe data out on STDOUT.

SHA1 encryption in Ruby

require 'digest/sha1'
sha1 = Digest::SHA1.hexdigest('something secret')
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS