4 # Copyright 2017 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Author: Aniruddha Atale
24 class StringShortner(object):
27 def __init__(self
, string
= None):
35 def string(self
, string
):
39 def short_string(self
):
41 return StringShortner
._get
_short
_string
(self
._string
)
46 def _fold_hex_series(series
):
49 for i
in range(int(length
/2)):
50 result
.append(series
[i
] ^ series
[(length
- 1) - i
])
53 result
.append(series
[int(length
/2) + 1])
58 def _num_from_hex_series(series
):
60 for i
in range(len(series
)):
66 def _get_short_string(string
):
67 sha
= hashlib
.sha384(string
.encode())
69 for i
in range(StringShortner
.FOLDS
):
70 digest
= StringShortner
._fold
_hex
_series
(digest
)
72 number
= StringShortner
._num
_from
_hex
_series
(digest
)
73 base62
= basehash
.base62(length
=StringShortner
.STRING_LEN
)
74 return base62
.hash(number
)