Thursday, November 22, 2018

SHA Encryption in IOS

Hi All,

Encryption has always been a key feature in apps developed in any Platform. Used widely in iOS Apps.

So lets today explore the SHA algorithm for encryption and decryption.


What is it

The algorithm is named as Secure Hash Algorithm. Highly used for Digital Signatures. This algorithm generates a unique and non-reversible text. Suppose if you are able to get the secret key used for hashing then alone with that you cannot generate the actual data making it secure. Also, two different kinds of data with the same secret hash code cannot generate the same hashed string.

However, with SHA1 two different strings with the same hash code generated the same hashed result resulting in a collision which led the users to move to SHA256 for better security. The larger is the bit size of hash the larger is the combination option and higher is the security but definitely, the implementation is important too.

But will longer hash length have any impact on performance? Yes, it may.


It can be broken down as

SHA[Number] [Output Size in Bits]

  • SHA0 [160]
  • SHA1 [160]
  • SHA2 [SHA224, SHA256, SHA384, SHA512]
  • SHA3 [SHA224, SHA256, SHA384, SHA512]





Plain Text >> Hash Algorithm >> Hashed Text



Utilize the Encryption logic in iOS App

1. Add Security Framework




2. Import CommonCrypto.h




Objective C

- (NSData *)hmacForKeyAndData:(NSString *)key data:(NSString *)data
{
    unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];

    const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];

    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

    return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

}


Swift

func hmac(forKeyAndData key: String?, data: String?) -> Data
{
    let cHMAC = [UInt8](repeating: 0, count: CC_SHA1_DIGEST_LENGTH)

    let cKey = Int8(key?.cString(using: String.Encoding.ascii.rawValue) ?? 0)
    let cData = Int8(data?.cString(using: String.Encoding.ascii.rawValue) ?? 0)

    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC) 

    return Data(bytes: &cHMAC, length: MemoryLayout<cHMAC>.size)
}

Thursday, November 1, 2018

Know your MongoDB

Hi All,

From past many weeks I had this eagerness to know what MongoDB is? & how it actually works. Brainstorming and learning lead to an inference that configuration is simple & easy, just follow some steps and start your journey in the new world.

So follow the trail to know what it actually is...


What is Mongo DB
A document based Database that stores Data in JSON Format. The fields can vary from document to document & YES its FREE and OPEN SOURCE.


Pros

  • Dynamic : There is no defined schema so your collection and its document can be of user's choice,  style & need.
  • No Joins required that makes it simple and easy to use
  • Flexible : Fields addition, updation is super easy and has less/no impact on he Application
  • Easy Integration & Configuration
  • It is document based & highly powerful
  • Cloud storage and distribution is key feature
  • High performance


Cons

  • Not perfectly ACID compliant
  • No stored procedure.
  • No auto-increment default option however can be achieved using Javascript code logic


Configuration

  • Download from https://www.mongodb.com/download-center
  • Alternative way to install using Homebrew as $ brew install mongodb 
  • You can configure  on local as well as remote machine
  • From 3.6 version of mongodb both mongod and mongos binaries are by default bind to LocalHost
  • Open your terminal
  • Unzip the downloaded TAR in a folder named Mongo
    • $ cd Mongo
    • $ tar-zxvf mongodb-osx-ssl-x86_64-4.0.3.tgz
  • Move
    • $ sudo mv mongodb-osx-ssl-x86_64-4.0.3 /usr/local/mongodb
  • Create a folder where Mongodb can store/write data, by default location is /data/db which needs to be created manually with proper permissions
    • $ sudo mkdir -p /data/db
    • $ sudo chown [LOGGED_IN_USER] /data/db
    • The default location can also be changed using 
      • $ mongod --dbpath /your_directory
  • To access mongodb commands you have to create a ~/.bash_profile file & assign /usr/local/mongodb/bin to $PATH environment variable
    • $ cd ~
    • $ pwd /users/[LOGGED_IN_USER]
    • $ touch .bash_profile
    • $ open .bash_profile 
      • Above command opens a text editor, paste below 2 commands in there and save
      • export MONGO_PATH=/usr/local/mongodb
      • export PATH=$PATH:$MONGO_PATH/bin
  • Restart the terminal 
  • $ mongo -version
  • $ mongod 
    • above command start the mongo daemon
  • Open another terminal window
    • $ mongo
  • Now in this terminal window you will see 
      MongoDB server version: 4.0.3
      Welcome to the MongoDB shell



What is Mongo

  • This is a JS shell interface for MongoDB which access the database


What is MongoD

  • This is a daemon, a host process for the database which runs mongodb process in background.
  • This has several default params associated with it
    • It runs on port 27017
    • Stores data in /data/db

What is bash profile & its usage in MongoDB
  • Its a hidden file that loads before the Terminal loads the shell environment
  • It contains shell configurations and preferences.
  • MongoDB paths are set in this file


Difference between SQL and Mongodb


Sql ServerMongoDB
DatabaseDatabase
TableCollection
IndexIndex
RowDocument
ColumnField
JoinsLink
PartitionRange Partition