3

Given mysql column is created under a table after extension installation. As mentioned, the size of VARCHAR column is 100.

$table_config->addColumn(
'value',
Varien_Db_Ddl_Table::TYPE_VARCHAR,
100,
array(
    'nullable' => false,
)

);

But when I try to save a created API key through model->save() method. The string gets truncated to 25 characters only.

$key = hash('sha256', 'mm_'.rand());
/save API Key in the configure page
    Mage::getModel('mm_sync/config')->setData(array(
            'name'  => 'api/key',
            'value' => $key,
        ))
        ->save();

The same value in the table:

+-----------+---------+--------------------------+
| config_id | name    | value                    |
+-----------+---------+--------------------------+
|         1 | api/key | 91XVVbiUBMDt40jZgKA51g== |

Any clue ?

1 Answer 1

2

Let's take them step by step

$key = hash('sha256', 'mm_'.rand());

this line is useless, because the value of $key is overwritten on the line below:

$key = substr(Mage::helper('core')->encrypt('mm_'.rand()), 0, 49);

this means that $key will contain the first 49 chars of the result of: Mage::helper('core')->encrypt('mm_'.rand());

If you look int the method Mage_Core_Helper_Data::encrypt you will see something like this:

public function encrypt($data)
{
    if (!Mage::isInstalled()) {
        return $data;
    }
    return $this->getEncryptor()->encrypt($data);
}

Assuming you are on community edition $this->getEncryptor()->encrypt($data) translates to Mage_Core_Model_Encryption::encrypt($data). This method looks like this:

public function encrypt($data)
{
    return base64_encode($this->_getCrypt()->encrypt((string)$data));
}

this means you should end up with a base64 encoded string. And the one you posted looks like one to me. the length of the generated string can be anything.

2
  • Apologies. One of the testing lines got left under the code snippet. But yes, you identified the problem. Thanks Commented Mar 12, 2015 at 7:53
  • Awesome track down of the problem, @Marius :) Commented Mar 12, 2015 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.