Darker Shade of the PhoneAccentBrush
While working with a darker shade of the phoneaccentbrush, I needed to maintain the uniformity of the shade across
different PhoneAccentBrushes available. To do this, I used the PhoneAccentBrush SolidColorBrush and added the generated
brush to the application resources. Here is how I did it.
In ShadeHelper.cs,
public static SolidColorBrush getDarkerShade(SolidColorBrush brush)
{
SolidColorBrush darkerShade = new SolidColorBrush();
byte alpha = 255;
byte red = (byte)(brush.Color.R - (brush.Color.R*0.4));
byte blue = (byte)(brush.Color.B - (brush.Color.B * 0.4));
byte green = (byte)(brush.Color.G - (brush.Color.G * 0.4));
darkerShade.Color = Color.FromArgb(alpha, red, green, blue);
return darkerShade;
}
In App.xaml.cs,
Resources.Add("PhoneDarkAccentBrush", ShadeHelper.getDarkerShade((Resources["PhoneAccentBrush"] as SolidColorBrush)));
Usage:
<TextBlock x:Name="darkAccent" Foreground={StaticResource PhoneDarkAccentBrush} Text="Darker Accent" />
Posted in windows phone 7 | Tagged application resources, phoneaccentbrush, solidcolorbrush, WP7 | Leave a comment
User defined variable names in ruby
A few months back, while porting some code from php to ruby, being a newbie that I am, I came across an issue.
The php code had a function as such:
public string functionName($var1, &$var2, $var3){}
Here the caller of the function gets to decide the name of the variable ($var2).
So, the caller can access the result from a variable name of the callers choice.
functionName("variablevalue", "variableName", "variableValue");
echo $variableName;
The code was procedural and had nothing to do with classes, methods or objects.
In ruby I had to provide a similar option where the caller can name the variable.
Here is how I had implemented this.
attr_accessor :userVar, :varName
Adding a couple of properties to the class. attr_accessor defines the get set methods.
def method_missing(meth, *x)
if "#{meth}" == self.varName
class << self
self
end.send :define_method, meth do |args|
self.userVar
end
self.send "#{meth}", x
else
puts "#{meth} : Such a method does not exist!"
end
end
This checks to see if the called method exists. Uses define_method .
def methodName(var1, userVar="", var3)
// function
self.userVar = userVar
self.varName = "#{self.userVar}"
self.userVar = self.result
return self.return
end
objectName.methodName(var1, "myvariable", var3) puts objectName.myVariable
The user input variable name is a new method/property which contains the result.
Posted in ruby | Tagged OOP, ruby | Leave a comment
AES Encryption (ECB, PKCS5) in Windows Phone 7
To support compatibility with some old software, I had to encrypt data with PKCS 5 padding and ECB mode with AES encryption. Awkward coder has made this a lot easier by creating a binary for windows phone 7.1 from bouncy castle. In this post I have an example of how I used the binary. Well, most of the code is from the original post by awkward coder.
public static string encrypt(string input, string password)
{
#region encrypt
var byteArray = Encoding.UTF8.GetBytes(input);
var key = Encoding.UTF8.GetBytes(password);
var encryptioncipher = CipherUtilities.GetCipher("AES/ECB/PKCS5PADDING");
encryptioncipher.Init(true, new KeyParameter(key));
//string algo = encryptioncipher.AlgorithmName;
int blocksize = encryptioncipher.GetBlockSize();
byte[] bytes = encryptioncipher.ProcessBytes(byteArray);
byte[] final = encryptioncipher.DoFinal();
string resultBytes = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
MemoryStream encryptedstream = new MemoryStream(bytes.Length + final.Length);
encryptedstream.Write(bytes, 0, bytes.Length);
encryptedstream.Write(final, 0, final.Length);
encryptedstream.Flush();
byte[] encryptedData = new byte[encryptedstream.Length];
encryptedstream.Position = 0;
encryptedstream.Read(encryptedData, 0, encryptedData.Length);
//Convert to hex string
string result = HexHelper.GetHextString(encryptedData);
return result;
#endregion
}
public static string decrypt(string encryptedhexstring, string password)
{
#region decrypt
//Covert from hexstring to bytearray
byte[] encryptedData = HexHelper.GetBytes(encryptedhexstring);
var key = Encoding.UTF8.GetBytes(password);
// AES algorthim with ECB cipher & PKCS5 padding...
var cipher = CipherUtilities.GetCipher("AES/ECB/PKCS5PADDING");
// Initialise the cipher...
cipher.Init(false, new KeyParameter(key));
// Decrypt the data and write the 'final' byte stream...
var decryptionbytes = cipher.ProcessBytes(encryptedData);
var decryptedfinal = cipher.DoFinal();
// Write the decrypt bytes & final to memory...
var decryptedstream = new MemoryStream(decryptionbytes.Length);
decryptedstream.Write(decryptionbytes, 0, decryptionbytes.Length);
decryptedstream.Write(decryptedfinal, 0, decryptedfinal.Length);
decryptedstream.Flush();
var decryptedData = new byte[decryptedstream.Length];
decryptedstream.Position = 0;
decryptedstream.Read(decryptedData, 0, (int)decryptedstream.Length);
// Convert the decrypted data to a string value...
var resultdecrypted = Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
return resultdecrypted;
#endregion
}
Posted in encryption, windows phone 7 | Tagged Awkward Coder, Bouncy Castle, ECB, PKCS5, WP7 | Leave a comment






