Answers to Software Questions the Experts Would Not Answer. Microsoft.NET, Java, Spring, Hibernate, JavaScript, C#, SQL Server as well as Mac, Solaris, Linux, CISCO IOS and related now include some Automotive IT! You could be having the same problem for hours like me!
Tuesday, April 12, 2011
LINQ To SQL DatabaseContext Insertion Memory Bloat & Clearing the Cache
You have inserted a huge number of records using the InsertOnSubmit(row) call, and you have ran out of memory in your .NET program using LINQ TO SQL technique.
Cause
The DatabaseContext caches all previously inserted rows.
Solution
There are many articles on the net using the Reflection locate the internal ClearCache then call it. This is not the intended use of the DatabaseContext object. It is a light-weight object and thus, you should simply dispose a object and create a new one. In my situation after a few thousand consecutive insertions, I dispose (i.e., new the object into the variable previously held the old context object.) This is also significantly more efficient way of clearing the cache held.
Friday, February 11, 2011
Installing Drupal 7.0 Experience on Joyent SmartMachine
- Creating the database and user is done from the Webmin->Servers->MySQL_Database_Server. Be sure that the you give the select, create, drop and insert permission to the mySQL user that you will give to Drupal
- SSH Login as the admin user then sudo bash
- The content of the drupal tar goes into /home/
/web/public so in other words in that directory, you should have install.php - PDO may be disabled (it was in my case). To enable it, edit /opt/local/etc/php.ini and uncomment pdo.so and pdo_mysel.so uncomment to mean to remove the semicolon.
- From the shell reboot the machine by tying in "reboot" This will activate the PDO (there are other ways to do this, this is one of the quick ways to do it.)
Sunday, February 06, 2011
Annoyed by: System.Xml: The node to be inserted is from a different document context?
You try to copy one or more XML Nodes (usually elements though) from one XMLDocument and another, and you get the following error:
(Note from 1/21/2012: Try using the XDocument class and LINQtoXML, it's much easier to deal with!)
- This contains the Text representation of XML including the node itself and all of the children.
- This contains the Text representation of XML node of all its children.
XmlDocument xd = new XmlDocument();var sx = sDoc.ToString();xd.LoadXml(sx);
Hope I gave you enough hint here, but if you need a more complete coding example, please post a comment and next time I come back here I will write one up for you.
Thursday, August 19, 2010
Notes on Amazon EC2 Image Creation Fails for Windows Machines
- This is my notes on the topic and not necessarily a complete solution. In fact, probably it contains some very inaccurate information (but you can help to rectify this via your comments.)
- This article also pertains to EBS Backed Windows EC2 running instances.
- To do this on Elasticfox, go to the Instances list, right click and select Create Images (AMI) item.
- Many people reported that when creating a copy from the running image, it will stop and restart the instance. This does happen. Worse yet, it will be stopped for the entire duration of when it is taking the snapshot, depending, this could be 10s of minutes. You'd probably want to schedule a down time. I should have done this just as soon as I have created the initial instance before went into production. Elasticfox does have a menu item not to reboot the instance I have not yet tried this.
- Some people say it has to be a Running instance, but that seems to be not true. I can image from a stopped instance as well and I have made images successfully this way via Elasticfox. In fact, that has more success rate than doing it on a running instance.
- Again, copying the running instance will cause a restart, so be prepared for the downtime.
- If you have already stopped the instance, it is at least more likely the imaging will succeed. A few caveats on this are that, at least in my case, the Elastic IP will get disassociated with the instance as shutting down the instance will detach the Elastic IP (wish they did not, at least for a short time), and also the machine's local addresses will change after restarting. This means that if you are locally pointing systems, for example, SQL, then the that address have to be re-configured in each application.
- Processes like SQL or other high I/O or CPU consumer items should be stopped. In fact, you should pare down the services to essentials (like SQL, agents, browser etc.) before taking the image copy. The one exception to this is not to stop the Amazon's EC2 services that came with the original Windows image whichever they exist.
- If you have other volumes mounted that also leads to a failure. Stop all services and applications you don't need to be running then dismount any "drives" and then make the image copy. This seems not be an issue if you are imaging a stopped instance.
Wednesday, August 18, 2010
IIS 6: Installing Duplicate Certificates to Multiple IIS 6 Servers For Server Farm Implementation
- Request and install the cert on the first server as you would normally do. Don't do the request from other servers, if you do, remove any pending cert requests.
- Open the MMC and add the Certificate module.
- Open the Computer Account then Personal certificate folder.
- Navigate down to the Web Certificate you want to export.
- Right click tasks and Export.
- In the wizard select to include the Private Key and include all certificates in the path in the next page.
- Copy the exported result to another server
- Do the same MMC stuff
- Import the stuff you exported into the Personal store
- From the IIS certificate section of the Directory Security, do the "Assign an Existing Certificate"
Getting an XmlNode (or XmlElement) Out of XPathNodeIterator (C#)
Get a Permission Error When Creating A New Scheduled Task
Friday, July 23, 2010
The Difference Between XmlNode and XmlElement
Sunday, July 18, 2010
Creating XMLDocument from Program Embedded String in C#
Example Code:
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader("<myNode>Hello</myNode>"));
Update: Well, that was doing too much, Load(string) would have worked just as well.
How To Use InsertAfter() in XmlDocument and Copy Nodes from another XmlDocument
You tried to insert an element (actually it has to be an XmlNode) using InsertAfter() or InsertBefore() method as doing so results in an exception with "The reference node is not a child of this node."
Solution Example:
The following code example demonstrates how InsertAfter() should (or at least could) be used, then add yet another node from another XmlDocument (created in-line in the code).
XmlDocument Insertion Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
namespace XMLDocumentExample
{
class Program
{
static void Main(string[] args)
{
// === EXAMPLE 1: How To Use InsertAfter in XMLDocuments ========================
XmlDocument doc1 = new XmlDocument();
// This XPathNavigator creates a convenient way to Write out the tree on the console.
// we don't use it for nothing else in this example.
XPathNavigator nav1 = doc1.CreateNavigator();
// This creates the root document, which all XML documents will need.
// i.e. <doc1></doc1>
XmlElement e = doc1.CreateElement("doc1");
doc1.AppendChild(e);
// Everything in thid document are the child of the root. I will purposely make a
// Mistake here so that I don't have doc1.2.0 element. I will demonstrate inserting
// doc1.2.0 using InsertAfter.
/*
*
* <doc1>
<doc1.0.0 />
<doc1.1.0 />
<doc1.3.0 />
</doc1>
*
*/
XmlElement c = doc1.CreateElement("doc1.0.0");
e.AppendChild(c);
c = doc1.CreateElement("doc1.1.0");
e.AppendChild(c);
c = doc1.CreateElement("doc1.3.0");
e.AppendChild(c);
Console.WriteLine(nav1.OuterXml); // Will show you you have doc1.0.0, doc1.1.0 and doc1.3.0 now.
// Now we will try to add the missing doc1.2.0 and just as a bonus I will attach doc1.2.1 as the
// child of doc1.2.0
// First let's get to the node AFTER where we will insert doc1.2.0, so that will be doc1.1.0
// Note you can either use /doc1/doc1.1.0 or //doc1.1.0 as the XPath query.
XmlNode nod1_1_0 = doc1.SelectSingleNode("//doc1.1.0");
// This block of code assembles doc1.2.0 node and its child doc1.2.1
XmlElement n2 = doc1.CreateElement("doc1.2.0");
XmlElement n2c = doc1.CreateElement("doc1.2.1");
n2.AppendChild(n2c);
// This is the most tricky important part. You have to ask the parent node to InsertAfter, and not
// directly with doc1.
//XmlNode myParent = nod1_1_0.ParentNode;
//myParent.InsertAfter(n2, nod1_1_0);
XmlNode mp = doc1.FirstChild;
mp.InsertAfter(n2, nod1_1_0);
// The result would look like this:
/*
<doc1>
<doc1.0.0 />
<doc1.1.0 />
<doc1.2.0>
<doc1.2.1 />
</doc1.2.0>
<doc1.3.0 />
</doc1>
* */
// Write the tree out on the console.
Console.WriteLine(nav1.OuterXml);
// === EXAMPLE 2: Copy A Node from One XMLDocument to Another ==============
// Create the Second XmlDocument, and then copy a node in the second
// document into the first document.
XmlDocument doc2 = new XmlDocument();
doc2.AppendChild(doc2.CreateElement("doc2"));
doc2.FirstChild.AppendChild(doc2.CreateElement("doc2.0.0"));
XPathNavigator nav2 = doc2.CreateNavigator();
Console.WriteLine(nav2.OuterXml);
XmlNode node_2_0_0 = doc2.SelectSingleNode("//doc2.0.0");
// Note that this does not append the node, it simply says that
// a foreign node can now belong to the first document.
XmlNode importableNode = doc1.ImportNode(node_2_0_0, true);
doc1.SelectSingleNode("/doc1/").AppendChild(importableNode);
Console.WriteLine(nav1.OuterXml);
Console.ReadLine();
}
}
}
*Note: Cloning of a node is required when you need to make a copy of a node and insert that back into the same XmlDocument. It appears that you cannot make a reference to a node and insert that (of course, if you can do that then there is a side effect of both node data changing if you edit the content of one node.)
Handling Customer or Press Complaints of Issues
- Gather as much facts and historical information about the performance of the product, for example issue call or email history from the customer or other customers, and the same for other similar customer set. Note: This is why you should rigorously record all issues using some type of database.
- Know the workarounds. In other words, don't even contact the customer or give a conference before you know the workarounds.
- Be prepared with the software patch but do not release it before you contact the customer. Since visibility of your effort is important, do not install it until the customer can expect to see the improvements.
- First, create a less stressful and friendly atmosphere. Tell jokes, other stuff.
- Begin with a positive tone by saying that your product is the best available, worked hard on it by best people and if you do have good reviews and so forth, tell them about that.
- Next, indicate that you are puzzled why the customer is making such claims to the best product available.
- But show that the customer is always right, so you say that "we have started to look into it."
- First, say: "Nothing is Perfect. You know that, and I know that."
- With not-so-surprised expression, talk about similar situations on other similar products. Tell customers that it is not just "our" product that is having the issue. Bring up the facts by video or verifiable statistics.
- With a surprised expression (if your number is actually lower), explain about the claim by other verifiable (or at least authentic) figures such as previous dissatisfaction statistics (number of issue tickets, number of returns).
- If you have a patch, inform the customer about it and then release or install it.
- Deny most of allegations, but admit a little
- Offer workarounds until the issue is completely solved, including refunds if that's possible.
Saturday, July 17, 2010
Unable to switch the encoding at System.Data.SqlClient.SqlConnection
- You have set up an SQL table with XML data type in it.
- You have a Strongly Typed DataSet
- You try to write XML from a C# String
- You get the following error
- In my case the real issue was that the actual strung was encoded in Unicode (UTF-16) but the XML header said utf-8. That mismatched.
- In your case the actual string may be encoded in some other format such as utf-8. Be sure to check. If the real encoding is the issue then you could re-encode your string from what you have into UTF-16. Look for System.Text.Encoding class on MSDN for further information.
Tuesday, July 13, 2010
Finding Uptime Using 'net statistics server' on Windows 2008 Server Does Not Work
Saturday, July 10, 2010
Ignoring .MySCMServerInfo in Visual Studio Web Project
- You are using Microsoft Visual Studio 2005, 2008, or 2010 with Seapine SurroundSCM.
- When you are working with Web project you see annoying .MySCMServerInfo file in each directory and furthermore, VisualStudio make you check them in when SurroundSCM does not want you to check them in. You don't get this issue when you are working on non-Web (e.g., "desktop" or class library) projects.
Monday, July 05, 2010
ASP.NET MVC2 New Project Fails To Build with 'The type or namespace name 'Controllers' does not exist in the namespace..."
Tuesday, June 08, 2010
Windows Server Hosts File Ignored or Not Looked Up
Friday, June 04, 2010
Google App iPhone Calendadar Does Now Show All Shared Calendars
Monday, May 31, 2010
You get [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified on 64-bit Windows
HP ILO 100 KVM Invalid username/password Error
Something has gone wild in the ILO Flash memory.
Fix
Friday, May 28, 2010
IMAP Problem with iPhone or iPOD for Google App
- They can read their email on the web
- They cannot suddenly access their email on iPhones even though IMAP and POP are enabled in their profile.
- May get "Cannot Connect Using SSL" message.
- Google has disabled IMAP (and POP) access probably due to some security issues such as repeated IMAP access with incorrect password.
- If this is the cause you won't be able to access your Google App email via IMAP or POP either, but most iPhone users don't realize this is happening since they may never use IMAP or POP clients on their home computers.
- Try doing "reset Captcha" and this should re-enable IMAP or POP thereby allow the access from iPhone or iPod Touch
Wednesday, October 14, 2009
Communicating with Busy People
- You send out a email to someone, say your manager asking a question. You have written 70 to 100 lines explaining everything she needs to know in a great detail so that she does not have to ask for more information.
- You feel that you have done everything right and wrote a courteous and detailed message. You also feel that you have chosen email as a courtesy as you know the person is really busy.
- You get no response, not even an acknowledgement. Now you feel that she is a jerk.
- "Gosh, another lengthy email from a colleague. I need to read it and understand what I need to do."
- First reaction, "Oh shit, another work to do on my list of things to do."
- But then she feels; "I know he is a hard worker and he means well, I don't want hurt his feeling."
- So, she decides: "I will respond to him when the meeting is over." (fat chance!)
- You have actually succeeded in reaching out to her by sending an email just before she went into a long meeting. Actually this timing plays a role in an effective communication.
- You did not know but you also acted like a jerk for sending a lengthy message, leaving her to interpret the email.
- You did not know what she thought about you when she opened the email. She actually did appreciate the work you put in, but then she felt like "how can I make this person more independent."
- You did not even know that she did not want to hurt your feelings. (This is actually very important psychology that you need to swallow.)
- You have made a request to her and that you have added extra work for her to do, one of them is to interpret this lengthy email.
- She is probably more motivated to this customer meeting than your email.
- She knows you so she is implicitly permitted to blow you off but not the customer. Not correct way of thinking but that's how it works.
- Write shorter messages and more often (but not too often). These days "chat" style of emailing is quite acceptable. My emails messages are usually not longer than 140 characters in length and for more info, I create a shared document and put a URL to it. I find it a bit of challenge in cramming in all the info in that space.
- Earlier part of the message, especially the Subject line of email is the most important part of the message.
- Do not compose a message that give a lot of work or interpretation on the recipient's part. As much as possible write a message saying exactly one thing she needs to do.
- Yes, one thing at at time! Never ever put more than one request in a message. Send a separate message at the right timing for the second stuff.
- If the action will benefit ultimately in her reputation or pay include that info too. It is mostly all about the motivation that drive people to do things.
- To this effect, I often use "Call For Action" keywords in the Subject line. In fact many of my email messages are complete message crammed in the subject line like: "Sarah: Sign the Check for ACME today." "Mike: Let's do Lunch Today at 12:00?" This way the recipient knows email is TO them and know exactly what to do. The message is right in front of email list and no need to open it, and action is right in the subject too. The message stands out clearly and talking to the person what needs to be done. To study Call For Action style communications, I recommend you read Google AdWords advise. Yes, basically you want a one click action and response from your recipient out of 100s of competing emails in her box!
- If it involves emotional discussions or expressions (for example, you are angry or concerned), do not write email. Call and leave a voicemail message. Voice can convey your emotions.
- Know that most other people may not manage their email box or voicemail box as well as you do. Emails are lost, buried or simply not looked at.
- Busy people are exactly that, they do not have time, so don't expect to get more time out of them.
- Busy people think the best way to deal with some things are to just leave them undone and not responding since by a response this will cause more work and responsibility to them.
- Emotions, Behaviors, and Motivations play a key role in the dynamics of human communications. This is where your the courtesy protocol that your parent taught you breaks down leaving you feeling like an neglected idiot. Of course you are not.
- Always communicate in short and exactly down to the point method of messaging often including "call for action" style messages. Do not write any more than 2 paragraphs. If there is more information, attach it as a file or point to a URL to your own blog or file download page... whatever technology you got.
- With emotional topics, use voicemail or better yet, talk directly to the person.
- People have been doing their people thing for at least 20, 30 or 40 years. Fat chance their behavior changes over-night. The best way to get through to people is to understand individual's motivation to me.
- Sometimes it does simply not work. In that case consider abandoning, move to another department, another customer, or another job.
- Finally, swallow the fact that neglecting is not personal, but people are simply just stretched to the max and do not have time. It is even be thought of as a friendly gesture not to hurt your feelings and an indication of trust that you won't get mad (or at least you won't express it immediately.)
Wednesday, October 07, 2009
JavaScript Note: Towards Building Client-Only App
Friday, September 25, 2009
Nortel Business Secure Router 222 and CISCO ASA 5500 Series VPN Connection
You would like to connect Nortel Small Business Router 222 to CISCO ASA 5505 or 5510 type device. You were probably not successful for a while (at least for me).
What Worked For Me:
- The major issue for me was to find what types of SA negotiation parameters to choose for the Phase 1. Pretty much this works only with 3DES-SHA1 combo for me. I tried others and won't work.
- Phase 2 appears to have not much problem whatever you choose.
- Also it seems that if you set the IKE->Policies : key lifetime to 86400 on the Nortel side they do not like.
- Go to the VPN menu on the left
- Edit or Create a new VPN Entry
- Connection Type: Branch Office
- Check Active
- NAT Transversal enabled
- Key management: IKE
- Negotiation Mode: mAIN
- Encapsulation Mode: Tunnel
- Authentication: Pre-Shared Key
- Local ID Type: IP
- Content: The Outside IP address of the Nortel
- Peer ID Type: IP
- Content: The Peer VPN Access point address of CISCO ASA
- My IP Address: The Outside IP address of the Nortel
- Secure Gatway Address: The Peer VPN Access point address of CISCO ASA
- ESP (Selected)
- Go to Advanced Menu
- Enable Replay Detection: Yes
- Phase 1
- Multiple Proposal: Not Checked
- Negotiation Mode: Main
- Encryption Algorithm: 3DES (most important, do not choose anything else)
- Authentication Algorithm: SHA1 (most important, do not choose anything else)
- SA Life Time (seconeds): 24000 (do not choose 86400)
- Key Group DH1 (but make sure that IKE Policies on the CISCO end has this combo)
- Phase 2
- Multiple Proposal: Not Checked
- Active Protocol: ESP
- Encryption Algorithm: ASE 256 (but can be 3DES)
- Authentication Algorithm: SHA1
- SA Life Time (Seconds): 24000 (do not use 86400)
- Encapsulation: Tunnel
- Perfect Forwarding Security: None (very important)
- Go to Configuration
- Open IKE->Policies node and be sure that 3des-sha DH group 1 pre-share authentication is in there. Lifetime(secs) can be left to 86400
- Now use the VPN Wizard to complete the rest.
Sunday, September 20, 2009
Snow Leopard Break Fix List
- Development environment (gcc, make etc): Requires a re-installation of Xcode that came with the Leopard CD.
- /usr/include/stdarg.h:4:25 Error when building something: Apprently needs OS 10.4 SDK installed and many stuff have to use gcc-4.0 to build. While updating Xcode, be sure to add that optional 10.4! Once you do that do
- macports: Get the Snow Leopard version from MacPorts web site.
- HP Printer Driver: See my previous post. If you HAD an HP printer then you may need to completely clean the driver software from your Mac.
- CISCO VPN Client 4.9.01: Re-installing will reactive the program.
Saturday, September 19, 2009
Snow Leopard and HP LaserJet 3020 Print Pauses After Upgrade
- Got the latest 6.0.1 Driver for HP is at http://support.apple.com/downloads/#macosx106
- Check: http://localhost:631/ which is the local CUPS driver admin page.
rm -rf /Library/Frameworks/HPDeviceModel.framework
rm -rf /Library/Frameworks/HPPml.framework
rm -rf /Library/Frameworks/HPServicesInterface.framework
rm -rf /Library/Frameworks/HPSmartPrint.framework
rm -rf /Library/LaunchAgents/com.hp.launchurlagent.plist
rm -rf /Library/Printers/hp folder
rm -rf /Library/Printers/PPDs/Contents/Resources/hp*.gz
rm -rf /System/Library/Extensions/hp_io_printerclassdriver_enabler.kext
Friday, September 18, 2009
ERROR: Cannot use the special principal 'sa'. Microsoft SQL Server, Error: 15405
People must be having this issue all over the world!
You tried to create or alter a table column (or likewise attributes in a table) with Microsoft SQL Server Management Studio and get the following error.
ERROR: Cannot use the special principal 'sa'. Microsoft SQL Server, Error: 15405
You can get into the SQL server with 'sa' in Mixed Mode authentication. You have even given the sa permissions to all of the databases.
Root Cause:
The database ownership is still not correct when you restored or re-attached database. This happens for example, if you restore a database using an integrated authentication account.
Try This Fix:
Open the SQL Query windows in the Microsoft SQL Server Management Studio, and try something that resemble below by changing. TheNameOfYourDatabase to the database you are using. Do this with every database that you have that you need the 'sa' access. This will change the owner of the database to the account you designate.
use TheNameOfYourDatabase
exec sp_changedbowner 'sa', 'true'
Wednesday, August 19, 2009
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
- Open the dataset designer
- Go to the TableAdapter in question
- Find where the primary key is set
- Right click over and select Primary Key menu
- Change the key to something else that make more sense
Sunday, July 19, 2009
Simple Way to Encrpt and Decrypt Short Text in Python
I was looking for some basic code in Python to encrypt and decrypt short text, for example, to store SQL passwords and such in configuration file or embed them within in scripts. So far I have not found a Python native way of doing this easily (most likely I am missing something and you ought to let me know). Also there are also some commercial libraries like CHILCAT that can do this using asymmetric stuff like AES and such, but I just want to hid obvious from plain public view (i.e., the local IT security people who'd scan all directories for passwords for holes) to prevent some robotic file scanners to collect such info.
I found the following code on the internet, but when I ran it under Python 2.6.2 it gave me a lot of errors, mainly due to the SHA library deprecated and moved into haslib. I made the corrections so here it is.
# Author: Paul Rubin, Fort GNOX Cryptography,
# Algorithmic advice from David Wagner, Richard Parker, Bryan
# Olson, and Paul Crowley on sci.crypt is gratefully acknowledged.
# Copyright 2002,2003 by Paul Rubin
# Copying license: same as Python 2.3 license
# Modified 19 July 2009 by Manabu Tokunaga for Python 2.6.2
# Please include this revision number in any bug reports: $Revision: 1.2 $.
from string import join
from array import array
#import sha
import hashlib
from time import time
class CryptError(Exception): pass
def _hash(str): return hashlib.sha224(str).digest()
_ivlen = 16
_maclen = 8
_state = _hash(`time()`)
# Author: Paul Rubin, Fort GNOX Cryptography,
# Algorithmic advice from David Wagner, Richard Parker, Bryan
# Olson, and Paul Crowley on sci.crypt is gratefully acknowledged.
# Copyright 2002,2003 by Paul Rubin
# Copying license: same as Python 2.3 license
# Modified 19 July 2009 by Manabu Tokunaga for Python 2.6.2
# Please include this revision number in any bug reports: $Revision: 1.2 $.
from string import join
from array import array
#import sha
import hashlib
from time import time
class CryptError(Exception): pass
def _hash(str): return hashlib.sha224(str).digest()
_ivlen = 16
_maclen = 8
_state = _hash(`time()`)
try:
import os
_pid = `os.getpid()`
except ImportError, AttributeError:
_pid = ''
def _expand_key(key, clen):
blocks = (clen+19)/20
xkey=[]
seed=key
for i in xrange(blocks):
seed=hashlib.sha224(key+seed).digest()
xkey.append(seed)
j = join(xkey,'')
return array ('L', j)
def p3_encrypt(plain,key):
global _state
H = _hash
# change _state BEFORE using it to compute nonce, in case there's
# a thread switch between computing the nonce and folding it into
# the state. This way if two threads compute a nonce from the
# same data, they won't both get the same nonce. (There's still
# a small danger of a duplicate nonce--see below).
_state = 'X'+_state
# Attempt to make nlist unique for each call, so we can get a
# unique nonce. It might be good to include a process ID or
# something, but I don't know if that's portable between OS's.
# Since is based partly on both the key and plaintext, in the
# worst case (encrypting the same plaintext with the same key in
# two separate Python instances at the same time), you might get
# identical ciphertexts for the identical plaintexts, which would
# be a security failure in some applications. Be careful.
nlist = [`time()`, _pid, _state, `len(plain)`,plain, key]
nonce = H(join(nlist,','))[:_ivlen]
_state = H('update2'+_state+nonce)
k_enc, k_auth = H('enc'+key+nonce), H('auth'+key+nonce)
n=len(plain) # cipher size not counting IV
stream = array('L', plain+'0000'[n&3:]) # pad to fill 32-bit words
xkey = _expand_key(k_enc, n+4)
for i in xrange(len(stream)):
stream[i] = stream[i] ^ xkey[i]
ct = nonce + stream.tostring()[:n]
auth = _hmac(ct, k_auth)
return ct + auth[:_maclen]
def p3_decrypt(cipher,key):
H = _hash
n=len(cipher)-_ivlen-_maclen # length of ciphertext
if n < 0:
raise CryptError, "invalid ciphertext"
nonce,stream,auth = \
cipher[:_ivlen], cipher[_ivlen:-_maclen]+'0000'[n&3:],cipher[-_maclen:]
k_enc, k_auth = H('enc'+key+nonce), H('auth'+key+nonce)
vauth = _hmac (cipher[:-_maclen], k_auth)[:_maclen]
if auth != vauth:
raise CryptError, "invalid key or ciphertext"
stream = array('L', stream)
xkey = _expand_key (k_enc, n+4)
for i in xrange (len(stream)):
stream[i] = stream[i] ^ xkey[i]
plain = stream.tostring()[:n]
return plain
# RFC 2104 HMAC message authentication code
# This implementation is faster than Python 2.2's hmac.py, and also works in
# old Python versions (at least as old as 1.5.2).
from string import translate
def _hmac_setup():
global _ipad, _opad, _itrans, _otrans
_itrans = array('B',[0]*256)
_otrans = array('B',[0]*256)
for i in xrange(256):
_itrans[i] = i ^ 0x36
_otrans[i] = i ^ 0x5c
_itrans = _itrans.tostring()
_otrans = _otrans.tostring()
_ipad = '\x36'*64
_opad = '\x5c'*64
def _hmac(msg, key):
if len(key)>64:
key=sha.new(key).digest()
ki = (translate(key,_itrans)+_ipad)[:64] # inner
ko = (translate(key,_otrans)+_opad)[:64] # outer
return hashlib.sha224(ko+hashlib.sha224(ki+msg).digest()).digest()
#
# benchmark and unit test
#
def _time_p3(n=1000,len=20):
plain="a"*len
t=time()
for i in xrange(n):
p3_encrypt(plain,"abcdefgh")
dt=time()-t
print "plain p3:", n,len,dt,"sec =",n*len/dt,"bytes/sec"
def _speed():
_time_p3(len=5)
_time_p3()
_time_p3(len=200)
_time_p3(len=2000,n=100)
def _test():
e=p3_encrypt
d=p3_decrypt
plain="test plaintext"
key = "test key"
c1 = e(plain,key)
c2 = e(plain,key)
assert c1!=c2
assert d(c2,key)==plain
assert d(c1,key)==plain
c3 = c2[:20]+chr(1+ord(c2[20]))+c2[21:] # change one ciphertext character
try:
print d(c3,key) # should throw exception
print "auth verification failure"
except CryptError:
pass
try:
print d(c2,'wrong key') # should throw exception
print "test failure"
except CryptError:
pass
_hmac_setup()
_test()
#_speed() # uncomment to run speed test
try:
import os
_pid = `os.getpid()`
except ImportError, AttributeError:
_pid = ''
def _expand_key(key, clen):
blocks = (clen+19)/20
xkey=[]
seed=key
for i in xrange(blocks):
seed=hashlib.sha224(key+seed).digest()
xkey.append(seed)
j = join(xkey,'')
return array ('L', j)
def p3_encrypt(plain,key):
global _state
H = _hash
# change _state BEFORE using it to compute nonce, in case there's
# a thread switch between computing the nonce and folding it into
# the state. This way if two threads compute a nonce from the
# same data, they won't both get the same nonce. (There's still
# a small danger of a duplicate nonce--see below).
_state = 'X'+_state
# Attempt to make nlist unique for each call, so we can get a
# unique nonce. It might be good to include a process ID or
# something, but I don't know if that's portable between OS's.
# Since is based partly on both the key and plaintext, in the
# worst case (encrypting the same plaintext with the same key in
# two separate Python instances at the same time), you might get
# identical ciphertexts for the identical plaintexts, which would
# be a security failure in some applications. Be careful.
nlist = [`time()`, _pid, _state, `len(plain)`,plain, key]
nonce = H(join(nlist,','))[:_ivlen]
_state = H('update2'+_state+nonce)
k_enc, k_auth = H('enc'+key+nonce), H('auth'+key+nonce)
n=len(plain) # cipher size not counting IV
stream = array('L', plain+'0000'[n&3:]) # pad to fill 32-bit words
xkey = _expand_key(k_enc, n+4)
for i in xrange(len(stream)):
stream[i] = stream[i] ^ xkey[i]
ct = nonce + stream.tostring()[:n]
auth = _hmac(ct, k_auth)
return ct + auth[:_maclen]
def p3_decrypt(cipher,key):
H = _hash
n=len(cipher)-_ivlen-_maclen # length of ciphertext
if n < 0:
raise CryptError, "invalid ciphertext"
nonce,stream,auth = \
cipher[:_ivlen], cipher[_ivlen:-_maclen]+'0000'[n&3:],cipher[-_maclen:]
k_enc, k_auth = H('enc'+key+nonce), H('auth'+key+nonce)
vauth = _hmac (cipher[:-_maclen], k_auth)[:_maclen]
if auth != vauth:
raise CryptError, "invalid key or ciphertext"
stream = array('L', stream)
xkey = _expand_key (k_enc, n+4)
for i in xrange (len(stream)):
stream[i] = stream[i] ^ xkey[i]
plain = stream.tostring()[:n]
return plain
# RFC 2104 HMAC message authentication code
# This implementation is faster than Python 2.2's hmac.py, and also works in
# old Python versions (at least as old as 1.5.2).
from string import translate
def _hmac_setup():
global _ipad, _opad, _itrans, _otrans
_itrans = array('B',[0]*256)
_otrans = array('B',[0]*256)
for i in xrange(256):
_itrans[i] = i ^ 0x36
_otrans[i] = i ^ 0x5c
_itrans = _itrans.tostring()
_otrans = _otrans.tostring()
_ipad = '\x36'*64
_opad = '\x5c'*64
def _hmac(msg, key):
if len(key)>64:
key=sha.new(key).digest()
ki = (translate(key,_itrans)+_ipad)[:64] # inner
ko = (translate(key,_otrans)+_opad)[:64] # outer
return hashlib.sha224(ko+hashlib.sha224(ki+msg).digest()).digest()
#
# benchmark and unit test
#
def _time_p3(n=1000,len=20):
plain="a"*len
t=time()
for i in xrange(n):
p3_encrypt(plain,"abcdefgh")
dt=time()-t
print "plain p3:", n,len,dt,"sec =",n*len/dt,"bytes/sec"
def _speed():
_time_p3(len=5)
_time_p3()
_time_p3(len=200)
_time_p3(len=2000,n=100)
def _test():
e=p3_encrypt
d=p3_decrypt
plain="test plaintext"
key = "test key"
c1 = e(plain,key)
c2 = e(plain,key)
assert c1!=c2
assert d(c2,key)==plain
assert d(c1,key)==plain
c3 = c2[:20]+chr(1+ord(c2[20]))+c2[21:] # change one ciphertext character
try:
print d(c3,key) # should throw exception
print "auth verification failure"
except CryptError:
pass
try:
print d(c2,'wrong key') # should throw exception
print "test failure"
except CryptError:
pass
_hmac_setup()
_test()
#_speed() # uncomment to run speed test
Thursday, July 02, 2009
Ruby on Rails: "no such file to load sqlite3" when preforming "rake db:create"
rake db:create
Thursday, June 25, 2009
Visual Studio 2005 & 2008: fatal error LNK1104: cannot open file 'LIBC.lib'
You tried to re-build an old Visual Studio 6.0 C or C++ project in Visual Studio 2005 or 2008 and you get the following error:
Fatal error LNK1104: cannot open file 'LIBC.lib'
Root Cause:
LIBC.LIB is no longer supported in the later version of Microsoft Visual Studio product.
Possible Fix:
Use the Multi-Thread-Safe version LIBCMT.LIB instead of LIBC.LIB that may fix.
- Go to the Project property page.
- Go to Linker on the left tree view panel
- Open the Input node
- In the "Additional Dependencies" you will find LIBC.lib, replace that with LIBMT.lib
Saturday, June 20, 2009
Mac OS Canon MP Series WiFi Printer Scanner Does Not Scan But Can Print
Friday, May 29, 2009
Yedda C# Twitter Update Results in 417 Expectation failed error
Machine Behind Firewall Gets: Failed auto update retrieval of third-party root list sequence number
Event Source: crypt32
Event Category: None
Event ID: 8
Description:
Failed auto update retrieval of third-party root list sequence number from:
Possible Fix:
Appears that it is lacking the Intermediate CA Certification.
Add a cert manually from: http://www.verisign.com/support/verisign-intermediate-ca/secure-site-pro-intermediate/index.html
Wednesday, May 27, 2009
Simple Regular Expressions Simple Wild Card Search
You are not only flabbagasted with so many people who just want to show off their admiration to the beaufy of Regular Expression and don't give you this simple most common use of regex!
Fix:
Try this:
.*foo.*doc
Just remember that when you could normally use a * in a DOS file search use the ".*" combo instead of just a * (. means almost any character and * means repeat as many of them that before the *). This will search files that are xfoo.doc and xfoodoc in your list.
By the way if you really really want to do *foo*.doc (escape) the extension period with a \ So to do a DOS/Shell search equivalent of *.doc you would do .*\.doc For me the above method is just adequate. But if you get the taste of .*\.doc then you are starting to tread to the zone of a regex geek. I'd just find the string, get done with it and rather go surfing myself!
If that does not work then you have other esoteric regex stuff in the string you are searching, like a " or a ? or a \ or { and such which have specific function in the expression. In that case, sorry, go RTFM! I am sorry!
Have fun!.
Tuesday, May 19, 2009
NET Remoting Error: Cannot create channel sink to connect to URL
You try to call an Activator.GetObject() to call a remote object.
Cannot create channel sink to connect to URL 'tcp:testhostname:1234/RemoteFileManager.soap'. An appropriate channel has probably not been registered. at System.Runtime.Remoting.RemotingServices.Unmarshal(Type classToProxy, String url, Object data) at System.Activator.GetObject(Type type, String url, Object state)
One Possible Cause:
Before going further and check the initializer and other stuff, double check the URL. In my case I forgot double slash after tcp:// and that caused above exception to happen.
Sunday, May 17, 2009
IDLE Autocomplete Does Not Work
You started to edit a Python program and Autocompletion does not work. You may have also noticed that it worked at one point or another.
Solution:
This is actually clearly stated in the Help document of the IDLE environment.
Just execute a program in IDLE (if you can). And it will load the symbols and after that automatic completion will work.
Note:
I looked for this quite a while on Google search but there was no clear explanation on this issue. I thought that some process goes to sleep or binding gets disabled.
On my Mac OS with Python 2.6, this happens every time I start IDLE up.
Monday, May 11, 2009
Visual Studio Remote Debugging Tips MS VS 2005
We have numerous situations at work where we need to debug issues at customer sites remotely. It is a huge hassle to install a development environment and source code and even source code control system at customer sites each time there is some hard-to-track crash or bug that we need to attend to.
Fortunately modern Visual Studio environment supports remote debugging. With the ubiquitous availability of VPNs this has became easier but there are still some challenges exist. I am sure that you have also been puzzled by why your system won't connect with the remote to begin a debug session, or get all sorts of different messages which all boil down to some connectivity and authentication issues.
I just got this to work (finally) with one of our customers using Visual Studio 2005 debugging environment. Both my system and the customer system was in different domains, so before I forget what hoops I had to jump through to get this going, I am going to document that here so that at least I can refer to this article myself.
I am sure that this is similar with 2008. We are using Windows XPs between the two.
The Most Important Gotcha Concept
The most important requirement for the remote debugging connection to work is the understanding of the following facts.
- It is a bidirectional communication and you cannot precisely control the user name and password for each way of the communication through the remote debugging tools. The logged-in user name and password should match on both remote and local debug environment. In other words, you want to log in to your local machine and the remote machine using exactly the same user name and password.
- You might say, other machine is in the domain and mine is not or likewise. But it does not actually seem to matter if the user name part (after the domain qualifier) and password pair is in the domain or not. The user name part and the password that goes with it must match on both ends.
- Note that if the user names are both in the domain and on the local system the password set in the domain will be used even if you are logged in as a local user. The interpretation of this is that "Administrator" should and cannot not be used in most sane situations since this is usually set by the domain's administrator and it would take an act of god to get the access to that password. But if you know what that is, change your local administrator's password as well as the local domain's password for the Administrator to get it to work. Not very advisable from the security stand-point though.
The Cookie Cutter Method
If remote debugging isn't working, at least some basic stuff going by creating some simple Hello World type app and use the following to see if you can connect and debug at all.
- On the LOCAL and REMOTE machine's user accounts, create the local user name account that DOES NOT exist in neither of the domains.
- Give that account the same password.
- Also it is best to edit C:\Windows\System32\Drivers\Etc\Hosts file and put each other system's WINS names and actual IP address. For example, if the remote machine is called XPWS1 then put that entry.
Windows firewalls may get in the way. I would temporarily turn this off while debugging the connection.
- The Local Security Settings (in Control Panel: Administrative Tools: Local Security Policy) must be tweaked on Windows XP as following. Go to Security Settings: Security Options: Network Access: Sharing and security model for local accounts. This must be changed to Classic - local users authenticate as themselves.
Monday, February 02, 2009
ActiveDirectory Recovery Note
When Windows 2000 came out I was a bit excited to be told about the fact that the AD does not have the notion of the Primary and Backup. But soon enough that was not true, and in fact it got even more complicated.
This was now 2008, in my small office, I had a brilliant idea of hosting two AD servers on virtual machines since we don't really maintain any more than a few user names and such. Well, however, we've just lost one of the controllers during a VM migration completely, so I had to basically create another one from the remaining "backup" controllers.
This has caused a several hours of "wasted time," since I really don't want delve into this AD stuff. So if this happens again here is the note to myself. Things like a backup fail-over should be basically instantaneous and I expect there ought to be one-button thingy for basically an idiotic (by choice) user by me, but this not being like that does protect lot of jobs in the industry I would guess.
- It seems to work better if you'd install the DNS first before adding the domain controller role.
- Download the Windows Support Tool from the MS web site. This has two important tools . One is called NETDIAG and another is DCDIAG. These are command line tools and if you run them without any argument they will tell you in more gory details about what's wrong with your DNS and Domain Controller settings. Basically they run a series of tests and they will return either pass or fail. The idea is to fix things so that they will all Pass. For each Fail, type in the error message into Google (not Live Search) and you will find the answer article on how to fix things on Microsoft web site. (Message to MS - Please improve the search on your own product related information.) I would fix the DNS first.
- Next tool you will need to use is NTDSUTIL program. You use this tool to basically transfer some of the critical roles that your lost domain controller had. The terminology you need to use to search is "Seize [blah blah] role" And you basically issue the Seize command to this tool. There are about 4 or 5 roles that you need to Seize, and one additional tricky thing is the "Infrastructure" role. This apparently should not be seized by the primary, so run the same tool on the secondary server and have it seize the "Infrastructure" role.