X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=common%2Fpython%2Frift%2Fmano%2Futils%2Fshort_name.py;fp=common%2Fpython%2Frift%2Fmano%2Futils%2Fshort_name.py;h=e4dd8a8220f5c9e6ec4d4470b7bc3df70dece269;hb=ee71ccf6da85650a8fbd2019293535082f017b78;hp=0000000000000000000000000000000000000000;hpb=9eae504f9fffaa02255241b1d93f6dc4b072926f;p=osm%2FSO.git diff --git a/common/python/rift/mano/utils/short_name.py b/common/python/rift/mano/utils/short_name.py new file mode 100644 index 00000000..e4dd8a82 --- /dev/null +++ b/common/python/rift/mano/utils/short_name.py @@ -0,0 +1,74 @@ +#!/usr/bin/python + +# +# Copyright 2017 RIFT.IO Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Author: Aniruddha Atale + +import hashlib +import basehash + + +class StringShortner(object): + FOLDS = 3 + STRING_LEN=9 + def __init__(self, string = None): + self._string = string + + @property + def string(self): + return self._string + + @string.setter + def string(self, string): + self._string = string + + @property + def short_string(self): + if self._string: + return StringShortner._get_short_string(self._string) + else: + return str() + + @staticmethod + def _fold_hex_series(series): + length = len(series) + result = list() + for i in range(int(length/2)): + result.append(series[i] ^ series[(length - 1) - i]) + + if length % 2: + result.append(series[int(length/2) + 1]) + + return result + + @staticmethod + def _num_from_hex_series(series): + result = 0 + for i in range(len(series)): + result = result * 256 + result += series[i] + return result + + @staticmethod + def _get_short_string(string): + sha = hashlib.sha384(string.encode()) + digest = sha.digest() + for i in range(StringShortner.FOLDS): + digest = StringShortner._fold_hex_series(digest) + + number = StringShortner._num_from_hex_series(digest) + base62 = basehash.base62(length=StringShortner.STRING_LEN) + return base62.hash(number)