A short guide to Homebrew

What is Homebrew

Homebrew is a package management system for Mac OS X.

How to install Homebrew

Step 1) Homebrew has a dependency on Xcode and Xcode command line tools. So first install latest Xcode and command line tools by going Xcode -> Preference, Choose “Downloads” tab and click on “install the Command Line Tools”

Step 2) Run this command in Terminal to install Homebrew –

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Now let’s go over a few common tasks you will have with Homebrew.

brew doctor

If you are doing any system update like OS X update or Xcode update you can check Homebrew’s health using this command. Homebrew will tell you if there is any issue with it or generally how to fix that issue.

brew update

This will update your Homebrew formula list. Please note that this will not update all your installed package it will just update the version and availability of packages you could install. You should run this often to keep your formula list updated or whenever you install/upgrade something new.

brew upgrade

This will update your installed packages.

brew install [package name here]

This will install new packages for you i.e.

brew install mysql

brew list

This will list all the packages that are currently installed on your system.

brew info [package name here]

This will show you information about a installed package.

brew uninstall [package name here]

The will remove package you previously installed.

So that’s is pretty much everything about Homebrew.

FacebookSDK/FacebookSDK.h file not found

If you are getting “FacebookSDK/FacebookSDK.h file not found” error, even if you have Facebook SDK included in your project? It might be possible that project is not able to access FacebookSDK because of some mis-configuration in references.

Your best bet to resolve this error is by  removing FacebookSDK.framework from your project and then start over with these steps. Re-linking the framework won’t help in many cases –

Step 1. Go to Build Phases in your Project Target.
Step 2. In Link Binary With Libraries, click the “+” button.
Step 3. Click on “Add Other…” button
Step 4. Browse your FacebookSDK folder. Generally in ~/Documents/FacebookSDK/
Step 5. Clik on (select) “FacebookSDK.framework” and then OPEN.

Hope this will save someone’s time!

Keyboard not working in iOS simulator?

If your keyboard (mac keyboard) is not working in the iPhone simulator you must have something wrong in iPhone Simulator preference file.

To fix the problem follow this –

0) Quit Xcode and simulator
1) Press ‘command+shift+g’ .. it will open the “go to folder” dialog.
2) type “~/Library/Preferences” in this dialog to go to your preference folder.
3) Delete “com.apple.iphonesimulator.plist” in this folder
4) Done. “com.apple.iphonesimulator.plist” will be regenerated when you start simulator again.

Alternatively you can also do this with just one command.

Open terminal and fire –

rm ~/Library/Preferences/com.apple.iphonesimulator.plist

This will do the trick in one step! Just make sure you quit Xcode and simulator before running this.

multiple SSH keys for github (or bitbucket) accounts & SSH Config

If you are using multiple github account for work and home projects or you are using one github account and one bitbucket account or any other git hosting service out there with SSH you have to manage SSH keys using a config file.

Lets go through each step for setting up github and bitbucket account-

Step 1: ssh keys

Create any keypairs you’ll need. In this example I’ve named these key pairs as ‘id_rsa_github’ and ‘id_rsa_’bitbucket:

ssh-keygen -t rsa -C “saurabhsharma@work.com"
ssh-keygen -t rsa -C “saurabhsharma@home.com”

Step 2: ssh config

Set up multiple ssh profiles by creating/modifying ~/.ssh/config. Note the differing ‘Host’ values:

# For GitHub
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github

# For BitBucket
Host bitbucket.org
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_bitbucket

Step 3: ssh-add

You may or may not have to do this. To check, list identity fingerprints by running:

$ ssh-add -l

2048 1f:1a:b8:69:cd:e3:ee:68:e1:c4:da:d8:96:7c:d0:6f saurabhsharma@home.com (RSA)
2048 6d:65:b9:3b:ff:9c:5a:54:1c:2f:6a:f7:44:03:84:3f saurabhsharma@work.com (RSA)

If your entries aren’t there then run:

ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_bitbucket

Step 4: test

To test you’ve done this all correctly, I suggest the following quick check:

$ ssh -T git@github.com

Hi Saurabh! You’ve successfully authenticated, but GitHub does not provide shell access.

I hope this will save someone’s hair!!

How to calculate MD5 hash in node.js

First a little intro to Hashes, for those who are very new to Hashes

What is a Hash?

A hash is a fixed-length string of bits that is procedurally and deterministially generated from some arbitrary block of source data. Some important properties of these hashes (the type useful for cryptography) include:

Fixed length: This means that, no matter what the input, the length of the hash is the same. For example, md5 hashes are always 128 bits long whether the input data is a few bits or a few gigabytes.

Deterministic: For the same input, you should expect to be able to calculate exactly the same hash. This makes hashes useful for checksums.

Collision-Resistant: A collision is when the same hash is generated for two different input blocks of data. Hash algorithms are designed to be extremely unlikely to have collisions — just how unlikely is a property of the hash algorithm. The importance of this property depends on the use case.

Unidirectional: A good hash algorithm is easy to apply, but hard to undo. This means that, given a hash, there isn’t any reasonable way to find out what the original piece of data was.

Some of the most common hash types are SHA1 and MD5

Now how to calculate Hash in Node.js

Node.js have a module named Crypto, which has a method called createHash which allows you to calculate a hash. Its only argument is a string representing the hash This example finds the md5 hash for the string, “I love Node.js”

var crypto = require('crypto');
var name = 'I love Node.js';
var hash = crypto.createHash('md5').update(name).digest("hex");

The update method is used to push data to later be turned into a hash with the digest method. update can be invoked multiple times to ingest streaming data, such as buffers from a file read stream. The argument for digest represents the output format, and may either be “binary”, “hex” or “base64”. It defaults to binary.