Source/NFX/ELink.cs
/// <summary>
/// Encodes a link into a textual form, using the supplied randomization seed, otherwise the system rnd is used.
/// A seed has 4 effective bits, yielding 16 possible variations for every link
/// </summary>
Encodes a link into a textual form, using the supplied randomization seed, otherwise the system rnd is used.
A seed has 4 effective bits, yielding 16 possible variations for every link
Encodes a link into a textual form using the supplied randomization seed otherwise the system rnd is used . A seed has 4 effective bits yielding 16 possible variations for every link
public string Encode(byte? seed = null) 
            {
                int rnd = (!seed.HasValue ? (byte)(ExternalRandomGenerator.Instance.NextRandomInteger & 0xff) : seed.Value) & VARIATIONS;
                var era = m_GDID.Era;
                var id = m_GDID.ID;
                var eraLength =
                     (era & 0xff000000) > 0 ? 4 :
                     (era &   0xff0000) > 0 ? 3 :
                     (era &     0xff00) > 0 ? 2 :
                     (era &       0xff) > 0 ? 1 : 0;
                var idLength =
                      (id & 0x0f00000000000000) > 0 ? 8 :  
                      (id &   0xff000000000000) > 0 ? 7 :
                      (id &     0xff0000000000) > 0 ? 6 :
                      (id &       0xff00000000) > 0 ? 5 :
                      (id &         0xff000000) > 0 ? 4 :
                      (id &           0xff0000) > 0 ? 3 :
                      (id &             0xff00) > 0 ? 2 : 1;
               var lead1 = (rnd << 4) | m_GDID.Authority;
               var lead2 = (eraLength << 4) | idLength;
               var crcValue = crc(era, id, m_Metadata);
                rnd |= rnd << 4;
                var result = new StringBuilder();
                result.Append( ALPHABET[ lead1 ] );
                result.Append( ALPHABET[ lead2 ^ rnd ]);
                result.Append( ALPHABET[ crcValue ^ rnd ]);
                result.Append( '-' );
                ulong seg = era;
                if (seg>0)
                {
                  for(var i=0; i<eraLength; i++)
                  {
                      result.Append( ALPHABET[ (int)(seg & 0xff)  ^ rnd ] );
                      seg >>= 8;
                  }
                }
                seg = id;
                for(var i=0; i<idLength; i++)
                {
                    if (i==4 && idLength>5)
                     result.Append( '-' );
                    result.Append( ALPHABET[ (int)(seg & 0xff)  ^ rnd ] );
                    seg >>= 8;
                }
                if (m_Metadata!=null)
                {
                   result.Append( '-' );
                   for(var i=0; i<m_Metadata.Length; i++)
                        result.Append( ALPHABET[ m_Metadata[i] ^ rnd ] );
                }
                var link = result.ToString();
                if (link.Length > MAX_LINK_CHAR_SIZE)
                  throw new NFXException(StringConsts.ELINK_CHAR_LENGTH_LIMIT_ERROR.Args(link.Substring(0, 20)));
                m_Link = link;
                return link;
            }
public string Encode ( byte ? seed = null ) { int rnd = ( ! seed . HasValue ? ( byte ) ( ExternalRandomGenerator . Instance . NextRandomInteger & 0xff ) : seed . Value ) & VARIATIONS ; var era = m_GDID . Era ; var id = m_GDID . ID ; var eraLength = ( era & 0xff000000 ) > 0 ? 4 : ( era & 0xff0000 ) > 0 ? 3 : ( era & 0xff00 ) > 0 ? 2 : ( era & 0xff ) > 0 ? 1 : 0 ; var idLength = ( id & 0x0f00000000000000 ) > 0 ? 8 : ( id & 0xff000000000000 ) > 0 ? 7 : ( id & 0xff0000000000 ) > 0 ? 6 : ( id & 0xff00000000 ) > 0 ? 5 : ( id & 0xff000000 ) > 0 ? 4 : ( id & 0xff0000 ) > 0 ? 3 : ( id & 0xff00 ) > 0 ? 2 : 1 ; var lead1 = ( rnd << 4 ) | m_GDID . Authority ; var lead2 = ( eraLength << 4 ) | idLength ; var crcValue = crc ( era , id , m_Metadata ) ; rnd |= rnd << 4 ; var result = new StringBuilder ( ) ; result . Append ( ALPHABET [ lead1 ] ) ; result . Append ( ALPHABET [ lead2 ^ rnd ] ) ; result . Append ( ALPHABET [ crcValue ^ rnd ] ) ; result . Append ( ' ' ) ; ulong seg = era ; if ( seg > 0 ) { for ( var i = 0 ; i < eraLength ; i ++ ) { result . Append ( ALPHABET [ ( int ) ( seg & 0xff ) ^ rnd ] ) ; seg >>= 8 ; } } seg = id ; for ( var i = 0 ; i < idLength ; i ++ ) { if ( i == 4 && idLength > 5 ) result . Append ( ' ' ) ; result . Append ( ALPHABET [ ( int ) ( seg & 0xff ) ^ rnd ] ) ; seg >>= 8 ; } if ( m_Metadata != null ) { result . Append ( ' ' ) ; for ( var i = 0 ; i < m_Metadata . Length ; i ++ ) result . Append ( ALPHABET [ m_Metadata [ i ] ^ rnd ] ) ; } var link = result . ToString ( ) ; if ( link . Length > MAX_LINK_CHAR_SIZE ) throw new NFXException ( StringConsts . ELINK_CHAR_LENGTH_LIMIT_ERROR . Args ( link . Substring ( 0 , 20 ) ) ) ; m_Link = link ; return link ; }
Encodes a link into a textual form, using the supplied randomization seed, otherwise the system rnd is used.
Encodes a link into a textual form using the supplied randomization seed otherwise the system rnd is used .
//props -> link /* Format
                 * [lead1][lead2][csum] - [... era ...] - [... id ...]{-[...metadata...]}
                 *  1bt     1bt    1bt       0..4bts        1..8bts         md-len bts
                 *
                 * [lead1] = [rnd][authority]  rnd 0..f
                 *            4bi     4bi
                 * [lead2] = [eraLen][idLen]  idLen is w/o authority
                 *            4bi     4bi

                 * [csum] = CRC32(era, id, md_len, metadata_bytes) % 0xff
                 *
                 * [era] = LitEndian uint32 0..4 bytes
                 * [id] = LitEndian 1..8 bytes LitEndian uint64 w/o authority
                 *
                 *
                 * [metadata] = 0..limited by MAX_LINK_CHAR_SIZE/2
                 *
                 */ //notice Authority exclusion in mask
seed
byte?