[20200103]GUID轉換GUID_BASE64.txt

来源:https://www.cnblogs.com/lfree/archive/2020/01/03/12143268.html
-Advertisement-
Play Games

[20200103]GUID轉換GUID_BASE64.txt--//最近在做一個項目優化,裡面大量使用uuid.優缺點在鏈接:http://blog.itpub.net.x.y265/viewspace-2670513/=>[20191225]主鍵使--//用uuid優缺點.txt 有相關討論.我 ...


[20200103]GUID轉換GUID_BASE64.txt

--//最近在做一個項目優化,裡面大量使用uuid.優缺點在鏈接:http://blog.itpub.net.x.y265/viewspace-2670513/=>[20191225]主鍵使
--//用uuid優缺點.txt 有相關討論.我自己的觀點不要濫用,或者講到處都用,合理使用才是比較正確的選擇.

--//昨天看12c相關書籍,發現oracle給每個PDB設置唯一GUID.我查看視圖V$CONTAINERs,發現有1個欄位GUID_BASE64,很明顯這個從guid轉
--//換過來,自己想知道這個轉換如何實現的?

1.環境:
[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==

--//註意看sys_guid()後面16位E0558253DD747177,居然沒有變化,不知道為什麼.
--//GUID_BASE64後面2位是字元'==',不可能3個正好都是==,一定是用來填充保持字元串長度24.

2.首先我必須確定GUID_BASE64的編碼:
--//base64,我的理解就是64進位,確定編碼很重要,最容易聯想到的rowid編碼也是64進位,是否其編碼與它一樣.
--//我檢索發現如下鏈接:
https://docs.oracle.com/cd/E18150_01/javadocs/DevelopmentKit/com/stc/connector/framework/util/Base64.html

--//內容如下:
            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
--//= 作為pad與看到結果一樣.

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.

編碼的輸出流必須以不超過76個字元的行表示。所有行打斷或其他解碼軟體必須忽略表1中沒有找到的字元。在base64數據中,除了那些
表1、斷行和其他空白可能表示傳輸錯誤,有關該錯誤的警告消息或在某些情況下,即使是拒絕信息也可能是合適的。

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.

如果在被編碼數據的末尾有少於24位可用,則執行特殊處理。一個完整的編碼量子總是在物體的末端完成。當一個輸入中可用的輸入位少
於24位時組,零位被添加(在右邊)形成一個6位組的整數。數據末尾的填充物是使用"="字元執行。由於所有base64輸入都是八進位的整數
,所以只有以下情況才能產生:
(1)編碼輸入的最終量子是24位的整數倍;這裡是編碼的最終單位輸出將是4個字元的整數倍,沒有"="填充.
(2)編碼輸入的最終數量是8位,編碼輸出的最終單位將是兩個字元,後面跟著兩個"="填充字元,或者
(3)編碼輸入的最終量子正好是16位;這裡,編碼輸出的最終單位是三位字元後面跟著一個"="填充字元。

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.

因為它只用於數據末尾的填充,所以任何"="字元的出現都可以作為數據結束的證據已經到達(在運輸過程中沒有截斷)。不可能有這樣的
保證,然而,當傳輸的八位數是三個的倍數,並且沒有"="字元存在時。

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

在base64編碼的數據中,base64字母表之外的任何字元都將被忽略。

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.

如果base64編碼直接應用於尚未轉換為規範形式。特別是,文本換行必須先轉換成CRLF序列。base64編碼。重要的是要註意的是,這可能
是由編碼器直接完成的,而不是在以前完成的。規範化步驟在一些實現中。

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.   

--//註:翻譯我使用金山詞霸,可能存在一些瑕疵...
--//這樣base64的編碼可以確定.A_Z a-z 0-9 +/

3.我寫一個測試腳本:
 $ 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

--//完全對不上.我再仔細看鏈接https://docs.oracle.com/cd/E18150_01/javadocs/DevelopmentKit/com/stc/connector/framework/util/Base64.html
--//說明.
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.

--//有1個非常明顯的提示 "zero bits are added (on the right) to form an integral number of 6-bit groups".
--//guid的顯示74A69DC145F5662BE0558253DD747177,占32字元.32*4 = 128 bits.
--//base64 相當於2^6,也就是6 bits表示1個64進位字元.
--//128/6 = 21.333,明顯無法整除,這樣結尾要補上1個0(占4bits).
--//(128+4)/6 = 22,這樣正好整除.
--//補上1個0再計算如下:

$ ./64base.sh 74A69DC145F5662BE0558253DD7471770
dKadwUX1ZivgVYJT3XRxdw

--//^_^正好對上.可以驗證看看.

$ 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==
--//對比完全能對上.當然不包括後面的兩個=.

4.修改腳本如下:
$ 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.總結:
--//純屬無聊,浪費一個下午探究這個問題.
--//在測試時我使用鏈接https://toolslick.com/conversion/data/guid的線上轉換工具,不然估計我無法猜測到如何實現.


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • `netstat`命令可以用來查詢整個系統的網路狀態。百度百科的定義如下: Netstat的 "定義" 是: Netstat是在 "內核" 中訪問網路連接狀態及其相關信息的 "程式" ,它能提供TCP連接,TCP和UDP監聽,進程 "記憶體管理" 的相關報告。 Netstat是 "控制台" 命令,是一 ...
  • 前幾天筆記本出現一個莫名其妙的問題,不勝其擾,具體表現如下: A、打開資源管理器(我的電腦)時,會出現“正在處理它”的Loading提示,且地址欄會出現綠色進度條,進度條一般會持續幾分鐘(不影響進入分區)。 B、資源管理器首頁的盤符圖標、快捷文件夾圖標經常要等10多秒才載入出來。 C、進入C/D/E ...
  • 本文簡述了基於CentOS 7.6安裝zabbix 4.0.4 的過程,yum為基於DVD1配置的本地yum源。 ...
  • A database management system (DBMS) is system software for creating and managing databases. The DBMS provides users and programmers with a systematic ... ...
  • select count(*),t.分組欄位 from (select t.* ,to_char(t.時間,'HH24') stime,to_char(t.時間,'HH24mi') fz,to_char(時間,'d') from A twhere 時間>=to_date('2019-12-01',' ...
  • 一.字元串函數 參數character_expression:由字元數據組成的字母數字表達式,可以是常量或變數,也可以是字元列或二進位數據列 參數integer_expression:是正整數,如果 integer_expression 為負,則返回空字元串 1.SubString():截取指定的字 ...
  • 規模增長之後,性能問題無穎是非常重要的,但重要的從來不只是性能。 ...
  • 環境信息: ||源庫|目標庫| |: |: |: | |操作系統|WIN7|WIN SVR 2012 R2| |IP|x.x.x.216|x.x.x.112| |資料庫版本|10.2.0.4.0 64bi|10.2.0.4.0 64bi| |存儲方式|單實例|單實例| |ORACLE_HOME|D: ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...