How to code a number in PHP

3

I would like to know how to display a coded number for the user.

I have several rows in my tables and I wanted to encode the user IDs. I was looking for functions for this but I only found md5 and sha1 but these generate very large keys.

Are there any that generate a short key (+/- 6 to 8 digits or characters)?

    
asked by anonymous 31.12.2014 / 02:10

2 answers

2

As you mentioned, the "security" case is preventing you from deducting the number of products or clients, so I suggest you use PHP. In this case you can create a function in a document and include this document in all necessary pages with include , examples:

Example of functions, using a simple sum (assuming the name is helper.php):

<?php
define('MASK_SUM', 1288900);

function mascararId($id_sem_mask) {
    return MASK_SUM + $id_sem_mask;
}

function desmascararId($id_com_mask) {
    return $id_com_mask - MASK_SUM;
}

And you can change the value of MASK_SUM when you want.

Using:

<?php
include 'helper.php';

$id = 10;

$maskId = mascararId($id);

echo 'Mascarado:', $maskId, '<br>';
echo 'Desmascarado:', desmascararId($maskId), '<br>';

Example online at ideone

    
31.12.2014 / 03:13
2

The use of a coded value and according to what I understood in your question, may well be a BITFIELD . If you look for your definition in WIKI you will find:

  

A bit field is a term used in computer programming to store multiple,   logical, neighboring bits, where each of the sets of bits, and single   bits can be addressed. A bit field is most commonly used to represent   integral types of known, fixed bit-width. A well-known usage of   bit-fields is a set of bits, and / or series of bits, known   the flags. [citation needed] For example, the first bit in a bit field   can be used to determine the state of a particular attribute   associated with the bit field.

In short ... you can ensure the use of multiple content in a single value (simplifying, using an integer that is more immediate). These can be BIT a BIT which defines states (on / off) or values in set of bits, for example in a set of 4 bits can store from 0 to 15 (decimal).

For the programmer a BITFIELD is very useful, because when implemented they greatly reduce the communications and the size of the information to write to a database. In a 32-bit integer we can, for example, pass 32 different states in only 4 bytes.

In your specific case, you put the question as identifier for records. Again this kind of mechanism will allow and depending on your imagination, put together more than one value. The example that I put I realized in 10 minutes and is for 32 bits but if you need bigger amplitudes can for example do the same for values 64 bits of 8 bytes. I created a bitfield32.php file and wrote this class with the same name:

class BitField32 {

    private $value;

    function __construct($value = 0) {
        $this->value = intval($value);
    }

    public function getHigh() {
        return ($this->value & 0xffff0000) >> 16;
    }

    public function getLow() {
        return $this->value & 0x0000ffff;
    }

    public function setHigh($value) {
        $hi16 = intval($value);
        if ($hi16 >= 0 && $hi16 < 0xffff) {
            $this->value = ($this->value & 0xffff) | ($value << 16);
        }
    }

    public function setLow($value) {
        $low16 = intval($value);
        if ($low16 >= 0 && $low16 < 0xffff) {
            $this->value = ($this->value & 0xffff0000) | $value;
        }
    }

    public function toHex() {
        return dechex($this->toHex());
    }

    public function toBits() {
        return decbin($this->value);
    }

    public function __toString() {
        return (string) $this->value;
    }

}

I think the code is very explanatory, so I did not document it in the hope that you understand what you are exposing. Now to use just need to ...

require 'BitField32.php';
$bf = new BitField32();
$bf->setHigh(365);
$bf->setLow(2015);

echo $bf, " ... "," high:", $bf->getHigh(), " low:", $bf->getLow();

According to this example, you can have a unique identifier, a 32-bit integer in the value of 23922655 and with it stores two fields of two distinct values that in the example are 365 and 2015 .

If the example is for a 64-bit integer we can for example have two 32-bit ID's and thus you can mask at most two 32-bit values.

In what I present I divide the 32 bits into two. The first 16 bits for one value and the second 16 bits for another value. But it does not have to be this way. Its limit is 32 bits but it can and depending on the desired implementation have more than one element.

I think so far have answered a part of your answer, but is still missing the MYSQL, which is partly answered, but the advantages? Is it really worth having this mechanism from MYSQL's point of view or already for any other database.

A field of INDEX in text vs. INDEX with a numeric, say the manuals and the experience that are numerically by far the best option especially in terms of performance. When we protect an implementation we must take this into account. When we look at large databases, we begin to realize the impact of using a mechanism such as BITFIELD . Nowadays the space of storage and communications in servers is paid, therefore are more reasons to add.

In conclusion, my answer is already long, and there is still a lot to talk about, and if the main reason is to mask values here, you have an effective solution, with the ability to add secure value in future implementations with clear advantages which does not just mask.

    
03.01.2015 / 13:46