Permalink
Browse files

First initial commit

  • Loading branch information...
0 parents commit e7ebe6bd35ca4f39dbe5e5338a1325eee000285e @tothimre committed Jan 8, 2014
No changes.
@@ -0,0 +1,15 @@
+language: php
+
+php:
+ - 5.3
+ - 5.4
+ - 5.5
+
+before_script:
+ - composer install --dev
+
+script:
+ - vendor/bin/phpunit tests
+
+#after_script:
+# - php vendor/bin/coveralls -v
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2014 Imre Toth
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,5 @@
+# Info Token
+
+[![Build Status](https://travis-ci.org/tothimre/hashids_timestamp_salt.png?branch=master)](https://travis-ci.org/flyingwhale/poc)
+
+This library perfectly capable to create api token identifier strings that can store a set of numbers. and from outside it looks completely random.
@@ -0,0 +1,30 @@
+{
+ "name": "tothimre/info_token",
+ "type": "library",
+ "description": "Generates Salted, timestamped tokens are different in different time intervalls, perfectly good for api tokens or sort of things like this. This can contain arbitrary information that you can decrypt at the server side.",
+ "keywords": ["token", "data storage", "hashids", "hash", "ids", "youtube", "bitly", "encrypt", "decrypt", "obfuscate"],
+ "license": "MIT",
+
+ "authors": [
+ {
+ "name": "Imre toth",
+ "email": "tothimre@gmail.com",
+ "homepage": "https://twitter.com/imtoth"
+ }
+ ],
+
+ "require": {
+ "php": ">=5.3.0",
+ "hashids/hashids": "0.3.*@dev"
+ },
+
+ "require-dev": {
+ "phpunit/phpunit": "3.7.*"
+ },
+
+ "autoload": {
+ "psr-0": {
+ "InfoToken": "src/"
+ }
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+namespace InfoToken\Hashids\Container;
+
+
+class HashidsContainers
+{
+ protected $hashids;
+
+ public function __construct(array $hashids)
+ {
+ $this->hashids = $hashids;
+ }
+
+ /**
+ * @param array $hashids
+ */
+ public function setHashids($hashids)
+ {
+ $this->hashids = $hashids;
+ }
+
+ /**
+ * @return array
+ */
+ public function getHashids()
+ {
+ return $this->hashids;
+ }
+
+}
@@ -0,0 +1,28 @@
+<?php
+namespace InfoToken\Hashids\Factory;
+
+use Hashids\Hashids;
+use InfoToken\HashidsConstructorValues;
+use InfoToken\TimeInterval;
+
+class HashsidsFactory
+{
+ const ACTUAL_INTERVAL_START_INDEX = 0;
+ const PREVIOUS_INTERVAL_START_INDEX = 1;
+
+ public static function getDecryptHashids(HashidsConstructorValues $hashidConstructorValues, TimeInterval $timeinterval)
+ {
+ return array(self::ACTUAL_INTERVAL_START_INDEX =>new Hashids($hashidConstructorValues->getSalt().$timeinterval->getIntervalStartTime(),
+ $hashidConstructorValues->getMinHashLength(), $hashidConstructorValues->getAlphabet()),
+ self::PREVIOUS_INTERVAL_START_INDEX =>new Hashids($hashidConstructorValues->getSalt().$timeinterval->getPreviousIntervalStartTime(),
+ $hashidConstructorValues->getMinHashLength(), $hashidConstructorValues->getAlphabet())
+ );
+ }
+
+ public static function getEncryptHashids(HashidsConstructorValues $hashidConstructorValues, TimeInterval $timeinterval)
+ {
+ return new Hashids($hashidConstructorValues->getSalt().$timeinterval->getIntervalStartTime(),
+ $hashidConstructorValues->getMinHashLength(), $hashidConstructorValues->getAlphabet());
+ }
+
+}
@@ -0,0 +1,38 @@
+<?php
+namespace InfoToken\Hashids\HashJugglers;
+
+use Hashids\Hashids;
+use InfoToken\Hashids\Container\HashidsContainers;
+
+class Checker
+{
+ public static function check(HashidsContainers $hashIdContainers, $stringToCheck)
+ {
+ $hashIds = $hashIdContainers->getHashids();
+ /** @var Hashids $hashId */
+ $hash = array();
+ foreach ($hashIds as $hashId){
+ $hash = $hashId->decrypt($stringToCheck);
+// var_dump($hash);
+ $checkerArray = Creator::getCheckerArray();
+ $hashCount = count($checkerArray);
+ $broken = false;
+ for($i=1;$i<=$hashCount;$i++)
+ {
+ if(array_shift($hash) != array_shift($checkerArray)){
+ $broken = true;
+ break;
+ }
+ }
+ if($broken){
+ continue;
+ }
+ if(!empty($hash)){
+ return $hash;
+ }
+ }
+ return $hash;
+
+ }
+
+}
@@ -0,0 +1,25 @@
+<?php
+namespace InfoToken\Hashids\HashJugglers;
+
+use Hashids\Hashids;
+use InfoToken\Hashids\Container\HashidsContainers;
+
+class Creator
+{
+ public static function create(Hashids $hashId, $numbersArray)
+ {
+ $hash = call_user_func_array(array($hashId, 'encrypt' ),array_merge(self::getCheckerArray(), $numbersArray));
+ return $hash;
+ }
+
+ public static function getCheckerArray(){
+ return array(
+ 123456,789012,345678,901234,567890,
+ 123456,789012,345678,901234,567890,
+ 123456,789012,345678,901234,567890,
+ 123456,789012,345678,901234,567890,
+ 123456,789012,345678,901234,567890,
+ 123456,789012,345678,901234,5
+ );
+ }
+}
@@ -0,0 +1,73 @@
+<?php
+
+namespace InfoToken;
+
+
+class HashidsConstructorValues
+{
+ protected $salt;
+
+ protected $minHashLength;
+
+ protected $alphabet;
+
+ protected $myCustomSalt = "";
+
+ function __construct($salt = '', $min_hash_length = 0, $alphabet = '')
+ {
+ $this->salt = $salt;
+ $this->minHashLength = $min_hash_length;
+ $this->alphabet = $alphabet;
+ }
+
+ /**
+ * @param mixed $alphabet
+ */
+ public function setAlphabet($alphabet)
+ {
+ $this->alphabet = $alphabet;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getAlphabet()
+ {
+ return $this->alphabet;
+ }
+
+ /**
+ * @param mixed $minHashLength
+ */
+ public function setMinHashLength($minHashLength)
+ {
+ $this->minHashLength = $minHashLength;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getMinHashLength()
+ {
+ return $this->minHashLength;
+ }
+
+ /**
+ * @param mixed $salt
+ */
+ public function setSalt($salt)
+ {
+ $this->salt = $salt;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getSalt()
+ {
+ return $this->salt;
+ }
+
+
+
+}
@@ -0,0 +1,42 @@
+<?php
+namespace InfoToken;
+
+class InfoToken
+{
+ /**
+ * @var \InfoToken\HashidsConstructorValues
+ */
+ protected $hashidsConstructorValues;
+
+ /**
+ * @var TimeInterval
+ */
+ protected $timeIntervalCalculator;
+
+ public function __construct(TimeInterval $timeIntervalCalculator, HashidsConstructorValues $hashidsConstructor = null)
+ {
+ $this->timeIntervalCalculator = $timeIntervalCalculator;
+
+ if(!$hashidsConstructor){
+ $this->hashidsConstructorValues = new HashidsConstructorValues();
+ }
+ }
+
+ /**
+ * @param \InfoToken\HashidsConstructorValues $hashidsConstructorValues
+ */
+ public function setHashidsConstructorValues($hashidsConstructorValues)
+ {
+ $this->hashidsConstructorValues = $hashidsConstructorValues;
+ }
+
+ /**
+ * @return \InfoToken\HashidsConstructorValues
+ */
+ public function getHashidsConstructorValues()
+ {
+ return $this->hashidsConstructorValues;
+ }
+
+
+}
@@ -0,0 +1,64 @@
+<?php
+namespace InfoToken;
+
+class TimeInterval
+{
+ const DEFAULT_INTERVAL = 300;
+ protected $intervalInSeconds;
+ protected $currentTimestamp;
+
+ public function __construct($IntervalInSeconds = self::DEFAULT_INTERVAL)
+ {
+ $this->currentTimestamp = time();
+ $this->intervalInSeconds = $IntervalInSeconds;
+ }
+
+ public function getPreviousIntervalStartTime()
+ {
+ return $this->getIntervalStartTime(-1);
+ }
+
+ public function getIntervalStartTime($shift = 0)
+ {
+ $item = ($this->currentTimestamp - $this->currentTimestamp % $this->intervalInSeconds);
+ return $item + $shift * $this->intervalInSeconds;
+ }
+
+ public function getNextIntervalStartTime()
+ {
+ return $this->getIntervalStartTime(1);
+ }
+
+ /**
+ * @param int $currentTimestamp
+ */
+ public function setCurrentTimestamp($currentTimestamp)
+ {
+ $this->currentTimestamp = $currentTimestamp;
+ }
+
+ /**
+ * @return int
+ */
+ public function getCurrentTimestamp()
+ {
+ return $this->currentTimestamp;
+ }
+
+ /**
+ * @param int $intervalInSeconds
+ */
+ public function setIntervalInSeconds($intervalInSeconds)
+ {
+ $this->intervalInSeconds = $intervalInSeconds;
+ }
+
+ /**
+ * @return int
+ */
+ public function getIntervalInSeconds()
+ {
+ return $this->intervalInSeconds;
+ }
+
+}
Oops, something went wrong.

0 comments on commit e7ebe6b

Please sign in to comment.