How to Bitwise compare a String

后端 未结 1 1726

I am working on a function that takes in a series of permission strings, less than 255 characters and assigns them to an entity. Each string assigned is unique, but there a

1条回答
  •  走了就别回头了
    2021-01-06 09:08

    I found an interesting solution but I am unsure if how logically correct it is because I am not very familiar with how PHP handles string data. I decided to cut away everything and try to do it straight without any hashing or assigning or whatnot and just do bitwise operations on strings. It seemed to work, but I am not sure I can prove my logic true enough.

    $key1 = "Access to Black Box";
    $key2 = "Managing Black Box";
    $key3 = "Nothing too see here";
    $key3a = "Nothingg B";
    $key3b = "too see";
    $glob = "";
    
    $glob = $glob | $key1;
    if(($glob & $key1) == $key1){echo "

    Key one exists in glob: " . $glob;} $glob = $glob | $key2; if(($glob & $key2) == $key2){echo "

    Key one exists in glob: " . $glob;} if(($glob & $key3) == $key3){echo "

    Key three exists in glob: " . $glob;} else{echo "

    Key three does not exists in glob: " . $glob;} $glob = $glob | $key3; if(($glob & $key3) == $key3){echo "

    Key three exists in glob: " . $glob;} if(($glob & $key3a) == $key3a){echo "

    Key three a exists in glob: " . $glob;} if(($glob & $key3b) == $key3b){echo "

    Key three b exists in glob: " . $glob;} else{echo "

    Key three b does not exists in glob: " . $glob;}

    Outputs:

    Key one exists in glob: Access to Black Box
    Key two exists in glob: Mcoew{nwobnmckkbox
    Key three does not exists in glob: Mcoew{nwobnmckkbox
    Key three exists in glob: Oomowoomsooboze
    Key three a exists in glob: Oomowoomsooboze
    Key three b does not exists in glob: Oomowoomsooboze
    

    So this works, but what would I be looking at collision wise? With key3a I showed that a string that has a combination of characters that match positions with characters in other keys I can get a false positive. But can I get around it with strict rules on the permission strings? Each resource type is named and each resource type has a limited number of associated permissions. So something like "Blog....Write Post", "Blog...Publish Post", "Blog....Moderate Post", "Podcast.......Upload", "Podcast.......Publish" to compensate for the increasing probability of a collision since string length has little impact on PHP's speed.

    | improve this answer | |

    0 讨论(0)
提交回复
热议问题