vniu / as3corelib

Automatically exported from code.google.com/p/as3corelib
0 stars 0 forks source link

Needed to split encryption function into two parts to enable saving hashed key for use in opening DB in Lita #105

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I just upgraded my encryption mechanism to the EncryptionKeyGenerator,
partly so my QA can open the database of the application we are developing
in Lita. In the admin menu activated only for company employees in the
application, I needed to be able to show them the hashed key for input into
Lita when opening the DB for testing. I had to make these changes to the
EncryptionKeyGenerator code to enable this. It basically splits the
algorithm into two parts so the code can access the intermediate hashed key
value for storage separately.

public function getEncryptionKey(password:String,
overrideSaltELSKey:String=null):ByteArray
        {
            var hashedKey:String = getHashedKey(password,overrideSaltELSKey);

            var encryptionKey:ByteArray = generateEncryptionKey(hashedKey);

            return encryptionKey;
        }

        public function
getEncryptionKeyForHashedKey(hashedKey:String):ByteArray
        {
            var encryptionKey:ByteArray = generateEncryptionKey(hashedKey);

            return encryptionKey;
        }

        public function
getHashedKey(password:String,overrideSaltELSKey:String=null):String
        {
            if (!validateStrongPassword(password))
            {
                throw new ArgumentError("The password must be a strong
password. It must be 8-32 characters long. It must contain at least one
uppercase letter, at least one lowercase letter, and at least one number or
symbol.");
            }

            if (overrideSaltELSKey != null && overrideSaltELSKey.length <= 0)
            {
                throw new ArgumentError("If an overrideSaltELSKey parameter
value is specified, it can't be an empty String.");
            }

            var concatenatedPassword:String = concatenatePassword(password);

            var saltKey:String;
            if (overrideSaltELSKey == null)
            {
                saltKey = SALT_ELS_KEY;
            }
            else
            {
                saltKey = overrideSaltELSKey;
            }

            var salt:ByteArray = EncryptedLocalStore.getItem(saltKey);
            if (salt == null)
            {
                salt = makeSalt();
                EncryptedLocalStore.setItem(saltKey, salt);
            }

            var unhashedKey:ByteArray = xorBytes(concatenatedPassword, salt);

            var hashedKey:String = SHA256.hashBytes(unhashedKey);

            return hashedKey;
        }

Original issue reported on code.google.com by arielj...@gmail.com on 7 May 2009 at 1:59