<?php
/**
* article url:http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/
*
* Translates a number to a short alhanumeric version
*
* Translated any number up to 9007199254740992
* to a shorter version in letters e.g.:
* 9007199254740989 --> PpQXn7COf
*
* specifiying the second argument true, it will
* translate back e.g.:
* PpQXn7COf --> 9007199254740989
*
* this function is based on any2dec && dec2any by
* fragmer[at]mail[dot]ru
* see: http://nl3.php.net/manual/en/function.base-convert.php#52450
*
* If you want the alphaID to be at least 3 letter long, use the
* $pad_up = 3 argument
*
* In most cases this is better than totally random ID generators
* because this can easily avoid duplicate ID's.
* For example if you correlate the alpha ID to an auto incrementing ID
* in your database, you're done.
*
* The reverse is done because it makes it slightly more cryptic,
* but it also makes it easier to spread lots of IDs in different
* directories on your filesystem. Example:
* $part1 = substr($alpha_id,0,1);
* $part2 = substr($alpha_id,1,1);
* $part3 = substr($alpha_id,2,strlen($alpha_id));
* $destindir = "/".$part1."/".$part2."/".$part3;
* // by reversing, directories are more evenly spread out. The
* // first 26 directories already occupy 26 main levels
*
* more info on limitation:
* - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372
*
* if you really need this for bigger numbers you probably have to look
* at things like: http://theserverpages.com/php/manual/en/ref.bc.php
* or: http://theserverpages.com/php/manual/en/ref.gmp.php
* but I haven't really dugg into this. If you have more info on those
* matters feel free to leave a comment.
*
* The following code block can be utilized by PEAR's Testing_DocTest
* <code>
* // Input //
* $number_in = 2188847690240;
* $alpha_in = "SpQXn7Cb";
*
* // Execute //
* $alpha_out = alphaID($number_in, false, 8);
* $number_out = alphaID($alpha_in, true, 8);
*
* if ($number_in != $number_out) {
* echo "Conversion failure, ".$alpha_in." returns ".$number_out." instead of the ";
* echo "desired: ".$number_in."\n";
* }
* if ($alpha_in != $alpha_out) {
* echo "Conversion failure, ".$number_in." returns ".$alpha_out." instead of the ";
* echo "desired: ".$alpha_in."\n";
* }
*
* // Show //
* echo $number_out." => ".$alpha_out."\n";
* echo $alpha_in." => ".$number_out."\n";
* echo alphaID(238328, false)." => ".alphaID(alphaID(238328, false), true)."\n";
*
* // expects:
* // 2188847690240 => SpQXn7Cb
* // SpQXn7Cb => 2188847690240
* // aaab => 238328
*
* </code>
*
* @author Kevin van Zonneveld <kevin@vanzonneveld.net>
* @author Simon Franz
* @author Deadfish
* @author SK83RJOSH
* @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
* @link http://kevin.vanzonneveld.net/
*
* @param mixed $in String or long input to translate
* @param boolean $to_num Reverses translation when true
* @param mixed $pad_up Number or boolean padds the result up to a specified length
* @param string $pass_key Supplying a password makes it harder to calculate the original ID
*
* @return mixed string or long
*/
function alphaID($in, $to_num = false, $pad_up = false, $pass_key = null)
{
$out = '';
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$base = strlen($index);

if ($pass_key !== null) {
// Although this function's purpose is to just make the
// ID short - and not so much secure,
// with this patch by Simon Franz (http://blog.snaky.org/)
// you can optionally supply a password to make it harder
// to calculate the corresponding numeric ID

for ($n = 0; $n < strlen($index); $n++) {
$i[] = substr($index, $n, 1);
}

$pass_hash = hash('sha256',$pass_key);
$pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash);

for ($n = 0; $n < strlen($index); $n++) {
$p[] = substr($pass_hash, $n, 1);
}

array_multisort($p, SORT_DESC, $i);
$index = implode($i);
}

if ($to_num) {
// Digital number <<-- alphabet letter code
$len = strlen($in) - 1;

for ($t = $len; $t >= 0; $t--) {
$bcp = bcpow($base, $len - $t);
$out = $out + strpos($index, substr($in, $t, 1)) * $bcp;
}

if (is_numeric($pad_up)) {
$pad_up--;

if ($pad_up > 0) {
$out -= pow($base, $pad_up);
}
}
} else {
// Digital number -->> alphabet letter code
if (is_numeric($pad_up)) {
$pad_up--;

if ($pad_up > 0) {
$in += pow($base, $pad_up);
}
}

for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) {
$bcp = bcpow($base, $t);
$a = floor($in / $bcp) % $base;
$out = $out . substr($index, $a, 1);
$in = $in - ($a * $bcp);
}
}

return $out;
}

php的id加密的更多相关文章

  1. STM32的FLASH ID加密

    #define FLASH_ID_OFFSET 30000    //任意定义一个数 //把地址直接减去或者加上一个数是不要程序中直接出现这个地址 volatile u32 Flash_ID_addr ...

  2. php利用32进制实现对id加密解密

    前言 最近在项目中遇到一个问题,当前用户分享一个邀请码给好友,好友根据邀请码注册成为新用户之后,则成为当前用户的下级,特定条件下,可以得到下级用户的一系列返利.这里要实现的就是根据当前用户的id,生成 ...

  3. 第五十一个知识点:什么是基于ID的加密的安全模型,然后描述一个IBE方案

    第五十一个知识点:什么是基于ID的加密的安全模型,然后描述一个IBE方案 在公钥密码学中,如果Alice想要给Bob发送一条消息,她需要Bob的公钥,一般来说公钥都很长,就像一个随机的字符串. 假设A ...

  4. 业务id转密文短链的一种实现思路

    业务场景: 买家通过电商app下单后,会受到一条短信,短信内容中包括改订单详情页面的h5地址连接,因为是出现在短信中,所以对连接有要求: 1.尽量短:2.安全性考虑,订单在数据库中对应的自增主键id不 ...

  5. Springboot使用自定义注解实现简单参数加密解密(注解+HandlerMethodArgumentResolver)

    前言 我黄汉三又回来了,快半年没更新博客了,这半年来的经历实属不易,疫情当头,本人实习的公司没有跟员工共患难, 直接辞掉了很多人.作为一个实习生,本人也被无情开除了.所以本人又得重新准备找工作了. 算 ...

  6. XSS Payload知识备忘

    参考资料:<白帽子讲Web安全>吴翰清 著 参见: 百度百科 http://baike.baidu.com/view/50325.htm 维基百科 http://zh.wikipedia. ...

  7. (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  8. SSL、TLS协议格式、HTTPS通信过程、RDP SSL通信过程

    相关学习资料 http://www.360doc.com/content/10/0602/08/1466362_30787868.shtml http://www.gxu.edu.cn/college ...

  9. iPhone 7-b

    iPhone 7就要出了!据悉,苹果秋季新品发布会将于9月7日举行,大家来看看iPhone7的概念设计有多逆天. 新机一出,大家最关心的都是价格问题,那就一起看看大家关注的价格问题: 4.7寸的iPh ...

随机推荐

  1. Chrome新发现

    昨晚写代码的时候惊喜的发现Chrome中能直接使用一些ES6的语法: let, const, 箭头函数等已经能直接使用. 酷酷的. 另外我的Chrome版本还是比较旧的,并不需要最新版本.

  2. 【转载】QPS,用户平均等待时间,服务器平均请求处理时间

    转自:http://www.cnblogs.com/coldplayerest/archive/2012/06/28/2567656.html 1. 计算网络的QPS时,必须要指定并发度,否则没有意义 ...

  3. mkdirp——递归创建目录及其子目录

    如果要创建目录A并创建目录A的子目录B,没有用-p参数的情况下mkdir会逐个创建目录(mkdir A; mkdir A/B); 加上参数-p就可以直接创建2个目录mkdir -p A/B( 如果目录 ...

  4. MySQL管理.md

    用户管理 创建 举例 mysql> create user test@localhost identified by 'password'; Query OK, 0 rows affected ...

  5. 代理错误[WinError 10061]

    操作过程: import urllib.request from urllib.error import URLError,HTTPError proxy_handler = urllib.reque ...

  6. Paxos算法简单陈述

    上文二段式和三段式提交协议是相对比较容易理解的.1990年Leslie Lamport 提出的Paxos算法是一种基于消息传递且具有高度容错特性的一致性算法.但是Paxos算法比较复杂,对于不能沉心学 ...

  7. HBase学习之路 (十)HBase表的设计原则

    建表高级属性 下面几个 shell 命令在 hbase 操作中可以起到很大的作用,且主要体现在建表的过程中,看 下面几个 create 属性 1. BLOOMFILTER 默认是 NONE 是否使用布 ...

  8. 20145203盖泽双《Java程序设计》第三周学习总结

    20145203盖泽双<Java程序设计>第三周学习总结 教材学习内容总结 1.两个基本的标准类:java.util.Scanner与java.math.BigDecimal. 2.Big ...

  9. 论文笔记 Beyond Part Models: Person Retrieval with Refined Part Pooling_ECCV_2018

    1. 摘要 使用part-feature 能够起到更好的效果,不过这个需要我们很好地定位part的位置. 本文中作者集中考虑part内部的一致性,提出了 part-based convolutiona ...

  10. PAT02-线性结构3 Reversing Linked List

    题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...