I'm wondering about the common practice of embedding things like hashing and encryption deep inside lower level code. Seems like it would be better to use some sort of object or macro convention so that security features can be easily assessed and updated as exploits are discovered and efficiencies made. For example, I see the following convention in PHP code dealing with authentication (blogs, code canyon, framework wikis, etc) ... here is a made up example to illustrate the point.
if ($myhash !== md5(shaX($this->key) . blah($this->salt) . blah($this->var))
instead of burying this down deep, wouldn't this be better as
if ($myhash != MY_HASH($key))
with MY_HASH in a config file or other easily accessible object, thus making it easier to update/maintain with better security as it becomes available? Why not put any nugget that encrypts or hashes into a config file or special hash file that contains only the transform functions?
Also - consider database access. PHP has so many abstractions, but I see applications doing this:
// grab some data from the database
if ($this->mongo_flag)
{
$this->mongo_abstraction
->where($blah1, $x)
->update($blah2);
}
elseif ($this->mysql_flag)
{
$this->mysql_abstraction
->where($blah1, $y)
->update($blah2);
}
elseif ($this->couch_flag)
{
$this->couch_abstraction
->where($blah1, $z)
->update($blah2);
}
Maybe only x,y,z are different.
Can't an object be instantiated that has the proper db method up front, thus eliminating the if/else logic, which is repeated everywhere a data base access is made?
i.e.
$mydata = $this->db_get_method($blah1, $blah2);
or
$mydata = $DB_GET_METHOD($db_type, $blah1, $blah2);
When if/else discrimination is preferred, then it seems like you should you skip the abstraction stuff and just use the native API making it more efficient and simpler to maintain since the native api probably isn't going to change and the abstraction is mostly nullified/voided by calling out each possible database type. Or not?
My main experience is with real-time embedded C programming (a lot of PHP code looks like procedural C designed with global structs) so I'm wondering whether performance might be the definitive answer, i.e. it just runs faster this way? Do objects introduce too much latency/complexity?