예제 #1
0
        private static string GenerateRootId(string name, Guid id)
        {
            const int maxPreservedNameLength = 22;

            if (name.Length < 2)
            {
                throw new ArgumentException("Value is too short", nameof(name));
            }

            var microHash = new HashidsNet.Hashids(ROOTID_SALT, 5, "abcdefghijklmnopqrstuvwxyz")
                            .EncodeHex(id.ToString("N")).Substring(0, 5);

            var rootId = $"{name}-{microHash}";

            if (name.Length > maxPreservedNameLength)
            {
                rootId = $"{name.Substring(0, maxPreservedNameLength)}-{microHash}";

                if (rootId.Contains("--"))
                {
                    rootId = rootId.Replace("--", "-");
                }
            }

            return(rootId.ToLowerInvariant());
        }
예제 #2
0
        /// <summary>
        /// CRC32のハッシュからからハッシュテキストを返す。
        /// </summary>
        /// <param name="hash">CRC32</param>
        /// <returns>ハッシュテキスト</returns>
        public static string ComputeHashIds(long hash)
        {
            //// hashidsでhashを算出する。
            HashidsNet.Hashids hashids = new HashidsNet.Hashids(Salt, 0, Alphabet);

            var result = hashids.EncodeLong(hash);

            return(result);
        }
예제 #3
0
        /// <summary>
        /// Used for creating small readable crypto-tokens
        /// </summary>
        /// <param name="minimumLength"></param>
        /// <param name="tokenHashAlphabet"></param>
        /// <returns></returns>
        public static string CreateEncodedHashId(int minimumLength, string tokenHashAlphabet)
        {
            var seed = new Random(BitConverter.ToInt32(CreateRandomBytes(4), 0)).Next(0, int.MaxValue);
            var salt = CreateRandomBytes(32);

            string saltString = Convert.ToBase64String(salt);

            var hashId = new HashidsNet.Hashids(salt: saltString, minHashLength: minimumLength, alphabet: tokenHashAlphabet);
            var hash   = hashId.Encode(seed);

            return(hash);
        }
예제 #4
0
 public static void Init(int inServerId, string inSalt, int inMinLen)
 {
     serverId = inServerId;
     if (serverId < 1)
     {
         serverId = 1;
     }
     if (string.IsNullOrWhiteSpace(inSalt))
     {
         inSalt = "ThisIsTheDefaultSalt";
     }
     if (inMinLen < 0)
     {
         inMinLen = 8;
     }
     hashIds = new HashidsNet.Hashids(inSalt, inMinLen, "BCDFHJKNPQRSTUVWXYZMEGA0123456789");
 }
예제 #5
0
        public ActionResult Confirmado(string codigo)
        {
            if (!String.IsNullOrWhiteSpace(codigo))
            {
                Simulado s = ListarSimuladoAbertoPorCodigo(codigo);
                if (s != null && s.FlagTemVaga && !s.CandidatoInscrito(Sessao.Candidato.CodCandidato))
                {
                    if (Sessao.Candidato.PerfilCompleto)
                    {
                        string numeroMascara = new HashidsNet.Hashids(Configuracoes.Recuperar("SIAC_SECRET") + s.Codigo, 6)
                                               .Encode(s.Ano, s.NumIdentificador, Sessao.Candidato.CodCandidato);

                        var candidato = new SimCandidato()
                        {
                            NumInscricao  = s.ObterNumInscricao(),
                            Candidato     = Sessao.Candidato,
                            DtInscricao   = DateTime.Now,
                            NumeroMascara = numeroMascara
                        };

                        s.SimCandidato.Add(candidato);

                        foreach (var prova in s.Provas)
                        {
                            prova.SimCandidatoProva.Add(new SimCandidatoProva()
                            {
                                SimCandidato = candidato,
                                SimProva     = prova
                            });
                        }

                        Repositorio.Commit();

                        string simuladoUrl = Url.Action("Inscricoes", "Candidato", new { codigo = s.Codigo }, Request.Url.Scheme);
                        EnviarEmail.Inscricao(Sessao.Candidato.Email, Sessao.Candidato.Nome, simuladoUrl, s.Titulo);

                        return(RedirectToAction("Inscricoes", "Candidato", new { codigo = s.Codigo }));
                    }
                    else
                    {
                        return(RedirectToAction("Perfil", "Candidato"));
                    }
                }
            }
            return(RedirectToAction("Index"));
        }
예제 #6
0
        public UserCache GetUserCache(string token)
        {
            UserCache user      = null;
            var       validator = new JwtSecurityTokenHandler();
            var       jwtToken  = validator.ReadJwtToken(token);
            var       userClaim = jwtToken.Claims.FirstOrDefault(ww => ww.Type == "userId");

            if (userClaim != null)
            {
                string hashCode = userClaim.Value;
                var    hashIds  = new HashidsNet.Hashids(salt: Salt.IdSalt);
                var    data     = hashIds.DecodeLong(hashCode);
                long   userId   = Convert.ToInt64(data[1]);
                user = GetUserCache(userId);
            }
            return(user);
        }
예제 #7
0
        /// <summary>
        /// テキストからHashを返す。
        /// </summary>
        /// <param name="text">テキスト</param>
        /// <returns>Hash</returns>
        public static string ComputeHashX(string text)
        {
            string result = string.Empty;

            byte[] bytes = Encoding.UTF8.GetBytes(text.Replace("-", string.Empty));
            //// テキストのCRC32を計算する。
            long crc = Crc32Algorithm.Compute(bytes);

            //// Encodeの入力は正の整数が必要なため絶対値を取る。
            crc = Math.Abs(crc);

            //// hashidsでhashを算出する。
            HashidsNet.Hashids hashids = new HashidsNet.Hashids(Salt, 0, Alphabet);

            result = hashids.EncodeLong(crc);

            return(result);
        }
예제 #8
0
        public string GenerateAccessToken(string authSecret, User user, DateTime accessTokenExpiration)
        {
            DateTimeOffset dto      = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero);
            var            unixDate = dto.ToUnixTimeMilliseconds();
            var            hashIds  = new HashidsNet.Hashids(salt: Salt.IdSalt);
            var            userCode = hashIds.EncodeLong(unixDate, user.Id);

            var claims = new[] {
                new Claim("userId", userCode),
            };

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authSecret));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken(claims: claims,
                                             expires: accessTokenExpiration,
                                             signingCredentials: creds);

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
예제 #9
0
 public HashidsService()
 {
     _hash = new HashidsNet.Hashids("Elephant has a big ears", 6);
 }
예제 #10
0
 public HashidService()
 {
     hash = new HashidsNet.Hashids("Some weird text", 2);
 }
예제 #11
0
 public HashBenchmark()
 {
     _hashids = new HashidsNet.Hashids();
 }