|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
public class SimpleCrypto { public static String encrypt(String seed, String cleartext) throws Exception { byte[] rawKey = getRawKey(seed.getBytes()); byte[] result = encrypt(rawKey, cleartext.getBytes()); return toHex(result); } public static String decrypt(String seed, String encrypted) throws Exception { byte[] rawKey = getRawKey(seed.getBytes()); byte[] enc = toByte(encrypted); byte[] result = decrypt(rawKey, enc); return new String(result); } private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed); kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; } private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(clear); return encrypted; } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } public static String toHex(String txt) { return toHex(txt.getBytes()); } public static String fromHex(String hex) { return new String(toByte(hex)); } public static byte[] toByte(String hexString) { int len = hexString.length()/2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue(); return result; } public static String toHex(byte[] buf) { if (buf == null) return ""; StringBuffer result = new StringBuffer(2*buf.length); for (int i = 0; i < buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private final static String HEX = "0123456789ABCDEF"; private static void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f)); } } |
Encrypt or Decrypt Strings
How to put one div in front of a other : HTML
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<code>If you want to display the content of one div over another, you can use the following code : <html> <body> <style> #div_1 { position:absolute; left:0px; top:0px; z-index:-1; } </style> <div id='div_1'> <img src="https://www.google.co.in/images/srpr/logo3w.png" > </div> <div style="color:#0000FF" id='div_2'> <p>This is some text.</p> </div> </body> </html></code> |
How to zoom in and zoom back to normal state : corona SDK
For this, just do as follows.
1: Create a scaleFactor:
local scale_factor = 0.2;
2: When you want to zoom out, do :
Your_object:scale(scale_factor,scale_factor)
3: When you want to zoom in, do :
Your_object:scale(1/scale_factor,1/scale_factor)
Getting the Child View Controllers of a View Controller in Xcode
We can get the child view controllers of a view controller using the childViewControllers property of the view controller.
If we are in a view controller class, if we want to get its child view controllers, then we can use the following code :
self.childViewControllers;
This will return an array of view controllers that are the child of the current view controller.
If there are no child view controllers of the current view controller, then the array will be empty.
If we are to get the child view controllers of a navigation controller, we can use the following code :
self.navigationController.childViewControllers;
Passing Multiple Parameters to PerformSelector Method in Xcode
It is possible to pass a single parameter to performSelector: method in Xcode using the following method :
[self performSelector:@selector(selectorNamewithArg:) withObject:param];
But what to do if we have to pass multiple parameters? There is a way to pass upto two parameters to the performSelector: method. It can be implemented using the following method :
[self performSelector:@selector(selectorNamewithArg1: Arg2:) withObject:param1 withObject:param2];
Here, two parameters are passed to the selector method.
LinkedHashMap example in Android
|
1 2 3 4 5 6 7 8 9 10 11 |
//Initializing LinkedHashMap LinkedHashMap<Key, Value> linkedHashMap = new LinkedHashMap<Key, Value>(); //add key and value linkedHashMap.put(key, value); //returns values linkedHashMap.values(); //return value of given key value = linkedHashMap.get(key); |
How to selects one of three possible rendering modes for the physics engine: corona SDK
There are types of rendering modes in corona SDK physics engine. The rendering modes are very much useful for the developers. Here I’ll show you how to choose any of these 3 rendering modes.
Code:
physics.setDrawMode( "debug" ) -- It will show collision engine outlines only.
physics.setDrawMode( "hybrid" ) -- It overlays collision outlines on normal Corona objects.
physics.setDrawMode( "normal" ) -- It is the default Corona renderer, with no collision outlines.
(Note: You can change the mode at any time, if you want)
as simple as that…
Associate variable usage in Smarty
You can create and assign associative array like following:
|
1 2 |
$smarty->assign(id,'name','age', 'job'); $smarty->assign('employee',array('id'=>1, 'name'=>'steve', 'age'=>40, 'job'=>'designer' )); |
In the template page:
|
1 2 3 4 5 6 7 8 9 10 11 |
<ul> <li>ID: {employee.id}</li> <li>Name: {employee.name}</li> <li>Age: {employee.age}</li> <li>Job: {employee.job}</li> </ul> |
To check if internet is available on android device
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* * @return boolean return true if the application can access the internet */ private boolean haveInternet(){ NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info==null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to disable internet while roaming, just return false return true; } return true; } |
To get the version of your application on the phone
|
1 2 3 4 5 6 7 8 9 |
public int getVersion(Context context) { try { PackageInfo pInfo = context.getPackageManager().getPackageInfo("here.thenameofyour.package", PackageManager.GET_META_DATA); return pInfo.versionCode; } catch (NameNotFoundException e) { return 0; } } } |



