[20200103] guid conversion_ BASE64.txt

Time:2020-9-24

[20200103] guid conversion_ BASE64.txt

–//Recently, I was doing a project optimization, which used UUID a lot http://blog.itpub.net.x.y265/viewspace-2670513/= >[20191225] primary key enables
–//I have a discussion about the advantages and disadvantages of uuid.txt. My own point of view should not be abused or used everywhere. Reasonable use is the right choice

–//Yesterday, I read 12C related books and found that Oracle set a unique guid for each PDB. I checked the view v $containers and found that there was a field guid_ Base64, it’s obvious that this is changed from GUI
–//In other words, I want to know how this transformation is realized?

1. environment:
[email protected]:1521/orclcdb> select banner from v$version;
BANNER
———————————————————————-
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 – Production

[email protected]:1521/orclcdb> select sys_guid() from dual ;
SYS_GUID()
——————————–
9B25CF226E3E36A5E0558253DD747177

[email protected]:1521/orclcdb>  select CON_ID,DBID,CON_UID,NAME,GUID,GUID_BASE64 from V$CONTAINERs;
CON_ID       DBID    CON_UID NAME     GUID                             GUID_BASE64
—— ———- ———- ——– ——————————– ————————
     1 2756091850          1 CDB$ROOT 64A52F53A7683286E053CDA9E80AED76 ZKUvU6doMobgU82p6Artdg==
     2 1474312904 1474312904 PDB$SEED 742DCFA2CE044FDEE0558253DD747177 dC3Pos4ET97gVYJT3XRxdw==
     3  115310104  115310104 ORCL     74A69DC145F5662BE0558253DD747177 dKadwUX1ZivgVYJT3XRxdw==

–//Pay attention to sys_ The following 16 bits of GUI () e0558253dd747177 have not changed. I don’t know why
–//GUID_ The last two bits of Base64 are the character ‘= =’, which cannot be exactly three = =. They must be used to fill in and keep the string length of 24

2. First of all, I have to determine the guid_ Base64 Code:
–//Base64, my understanding is 64 base. It is very important to determine the code. The most easily associated ROWID code is 64 base. Is the code the same as it
–//I searched and found the following links:
https://docs.oracle.com/cd/E18150_01/javadocs/DevelopmentKit/com/stc/connector/framework/util/Base64.html

–//The contents are as follows:
            Table 1: The Base64 Alphabet
Value Encoding  Value Encoding  Value Encoding  Value Encoding
   0 A            17 R            34 i            51 z
   1 B            18 S            35 j            52 0
   2 C            19 T            36 k            53 1
   3 D            20 U            37 l            54 2
   4 E            21 V            38 m            55 3
   5 F            22 W            39 n            56 4
   6 G            23 X            40 o            57 5
   7 H            24 Y            41 p            58 6
   8 I            25 Z            42 q            59 7
   9 J            26 a            43 r            60 8
  10 K            27 b            44 s            61 9
  11 L            28 c            45 t            62 +
  12 M            29 d            46 u            63 /
  13 N            30 e            47 v
  14 O            31 f            48 w         (pad) =
  15 P            32 g            49 x
  16 Q            33 h            50 y
–//=As pad, it is the same as seeing the result

The encoded output stream must be represented in lines of no more than 76 characters each. All line breaks or other
characters no found in Table 1 must be ignored by decoding software. In base64 data, characters other than those in
Table 1, line breaks, and other white space probably indicate a transmission error, about which a warning message or
even a message rejection might be appropriate under some circumstances.

The encoded output stream must be represented in lines of no more than 76 characters. All line breaks or other decoding software must ignore characters not found in Table 1. In Base64 data, except for those
Table 1, broken lines and other blanks may indicate a transmission error, and warning messages about the error or, in some cases, even denial information may be appropriate.

Special processing is performed if fewer than 24 bits are available at the end of the data being encoded. A full
encoding quantum is always completed at the end of a body. When fewer than 24 input bits are available in an input
group, zero bits are added (on the right) to form an integral number of 6-bit groups. Padding at the end of the data is
performed using the “=” character. Since all base64 input is an integral number of octets, only the following cases can
arise: (1) the final quantum of encoding input is an integral multiple of 24 bits; here, the final unit of encoded
output will be an integral multiple of 4 characters with no “=” padding, (2) the final quantum of encoding input is
exactly 8 bits; here, the final unit of encoded output will be two characters followed by two “=” padding characters, or
(3) the final quantum of encoding input is exactly 16 bits; here, the final unit of encoded output will be three
characters followed by one “=” padding character.

If less than 24 bits are available at the end of the encoded data, special processing is performed. A complete coding quantum is always done at the end of the object. When there are few input bits available in an input
At 24 bit, the zero bit is added (on the right) to form a 6-bit integer. The padding at the end of the data is executed using the “=” character. Because all Base64 inputs are octal integers
Therefore, only the following situations can occur:
(1) The final quantum of the input is an integer multiple of 24 bits; here is the final unit output of the encoding will be an integer multiple of 4 characters without “=” filling
(2) The final number of coded inputs is 8 bits, and the final unit of coded output will be two characters followed by two “=” padding characters, or
(3) The final quantum of encoded input is exactly 16 bits; here, the final unit of coded output is three characters followed by an “=” padding character.

Because it is used only for padding at the end of the data, the occurrence of any “=” characters may be taken as
evidence that the end of the data has been reached (without truncation in transit). No such assurance is possible,
however, when the number of octets transmitted was a multiple of three and no “=” characters are present.

Because it is only used for padding at the end of the data, any occurrence of the “=” character can be used as evidence of the end of the data has arrived (no truncation during transportation). There can’t be such a thing
Guaranteed, however, when the transmitted octet is a multiple of three and no “=” character exists.

Any characters outside of the base64 alphabet are to be ignored in base64-encoded data.

In Base64 encoded data, any characters outside the base64 alphabet are ignored.

Care must be taken to use the proper octets for line breaks if base64 encoding is applied directly to text material that
has not been converted to canonical form. In particular, text line breaks must be converted into CRLF sequences prior to
base64 encoding. The important thing to note is that this may be done directly by the encoder rather than in a prior
canonicalization step in some implementations.

If Base64 encoding is applied directly to the application that has not yet been converted to the canonical form. In particular, text wrapping must first be converted to a CRLF sequence. Base64 encoding. It is important to note that this may
It is done directly by the encoder, not before. Normalization steps are in some implementations.

NOTE: There is no need to worry about quoting potential boundary delimiters within base64-encoded bodies within
multipart entities because no hyphen characters are used in the base64 encoding.   

–//Note: I use Kingsoft Ciba, there may be some flaws
–//In this way, the encoding of Base64 can determine. A_ Z a-z 0-9 +/

3. I write a test script:
 $ cat 64base.sh
#! /bin/bash
v2=$1
BASE64=($( echo {A..Z} {a..z} {0..9} + / ))

res=”
for i in $(echo “obase=64;ibase=16; $v2” | bc| tr -d ‘\\\r\n’)
do
    res=${res}${BASE64[$(( 10#$i ))]}
done

echo $res

$ ./64base.sh 74A69DC145F5662BE0558253DD747177
B0pp3BRfVmK+BVglPddHF3

–//It’s totally wrong. I’ll take a closer look at the link https://docs.oracle.com/cd/E18150_ 01/javadocs/DevelopmentKit/com/stc/connector/framework/util/Base64.html
–//Description
When fewer than 24 input bits are available in an input group, zero bits are added (on the right) to form an integral
number of 6-bit groups. Padding at the end of the data is performed using the “=” character.

–//There is a very obvious hint “zero bits are added (on the right) to form an integral number of 6-bit groups”
–//The display of GUID is 74a69dc145f5662be0558253dd747177, which takes 32 characters.32 * 4 = 128 bits
–//Base64 is equivalent to 2 ^ 6, that is, 6 bits represents a 64 base character
–//128 / 6 = 21.333, which is obviously not divisible, so a 0 (4 bits) should be added to the end
–//(128 + 4) / 6 = 22, which is exactly divisible
–//Add 1 0 and calculate as follows:

$ ./64base.sh 74A69DC145F5662BE0558253DD7471770
dKadwUX1ZivgVYJT3XRxdw

–//^_ ^It’s just on. You can verify it

$ echo 64A52F53A7683286E053CDA9E80AED760 742DCFA2CE044FDEE0558253DD7471770 74A69DC145F5662BE0558253DD7471770 | tr ‘ ‘ ‘\n’ | xargs -IQ ./64base.sh Q
ZKUvU6doMobgU82p6Artdg
dC3Pos4ET97gVYJT3XRxdw
dKadwUX1ZivgVYJT3XRxdw

[email protected]:1521/orclcdb>  select CON_ID,DBID,CON_UID,NAME,GUID,GUID_BASE64 from V$CONTAINERs;
CON_ID       DBID    CON_UID NAME     GUID                             GUID_BASE64
—— ———- ———- ——– ——————————– ————————
     1 2756091850          1 CDB$ROOT 64A52F53A7683286E053CDA9E80AED76 ZKUvU6doMobgU82p6Artdg==
     2 1474312904 1474312904 PDB$SEED 742DCFA2CE044FDEE0558253DD747177 dC3Pos4ET97gVYJT3XRxdw==
     3  115310104  115310104 ORCL     74A69DC145F5662BE0558253DD747177 dKadwUX1ZivgVYJT3XRxdw==
–//The comparison can be made completely. Of course, the latter two are not included

4. the script is modified as follows:
$ cat o64base.sh
#! /bin/bash
# convert guid to guid_base64
odebug=${ODEBUG:-0}

v2=${1}0
BASE64=($( echo {A..Z} {a..z} {0..9} + / ))

res=”
for i in $(echo “obase=64;ibase=16; $v2” | bc| tr -d ‘\\\r\n’)
do
    res=${res}${BASE64[$(( 10#$i ))]}
done

if [ $odebug -eq 1 ] ; then
    echo v2=$v2 res=$res
fi

res=${res}==
echo $res

$ ./o64base.sh 74A69DC145F5662BE0558253DD747177
dKadwUX1ZivgVYJT3XRxdw==

5. Summary:
–//It’s just boring. It’s a waste of an afternoon exploring this problem
–//I use links during testing https://toolslick.com/conversion/data/guid I can’t guess how to implement it