harry’s memorandum

おれおれメモ

base64の備忘録

とりあえず、base64をしたかったのですが、調べると色々な方法があるなぁ、と思ったので備忘録。

opensslコマンド

一番よく使う方法かな。

$ echo -n dharry | openssl enc -base64
ZGhhcnJ5
$ echo  "ZGhhcnJ5" | openssl  enc -d -base64; echo
dharry
base64コマンド

Linuxだとこれかな。coreutilsは便利だけど他のUNIXだとなかったりするんだな。

$ echo -n dharry | base64
ZGhhcnJ5
$ echo -n "ZGhhcnJ5" | base64 -d; echo
dharry
$ rpm -qf /usr/bin/base64
coreutils-5.97-34.el5_8.1
nkfコマンド

これはこれで便利

$ echo -n "dharry"| nkf  -MB; echo
ZGhhcnJ5
$ echo -n "ZGhhcnJ5"| nkf -mB; echo
dharry
perl

perlはどのUNIXでもあるから一番楽かも。でもHP-UX11.00ぐらいだと厳しい。

$ echo -n "dharry" | perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'
ZGhhcnJ5
$ echo -n "ZGhhcnJ5" | perl -MMIME::Base64 -ne 'print decode_base64($_)'; echo
dharry
ruby
$ echo -n dharry   | ruby -r base64 -e 'puts Base64.encode64(STDIN.read)'
ZGhhcnJ5
$ echo -n ZGhhcnJ5 | ruby -r base64 -e 'puts Base64.decode64(STDIN.read)'
dharry
python
$ echo -n dharry | python -c "import base64,sys; base64.encode(sys.stdin,sys.stdout)"
ZGhhcnJ5
$ echo -n ZGhhcnJ5 | python -c "import base64,sys; base64.decode(sys.stdin,sys.stdout)"
dharry
PowerShell

コマンドレットは便利と思ったけどいまだになじめない。慣れは重要ですね。

PS C:\Users\dharry> $str = "dharry"
PS C:\Users\dharry> $str =  [Convert]::ToBase64String([System.Text.Encoding]::GetEncoding("iso-2022-jp").GetBytes($str))
PS C:\Users\dharry> $str
ZGhhcnJ5
tcl

tclshで試してみた。centosのtclだとbase64ライブラリがなかったのでActiveStateのActiveTCL8.5.11.1で確認。

% package require base64
2.4.2
% base64::encode "dharry"
ZGhhcnJ5
curl

apachebasic認証に渡すときはbase64にしているのでwgetcurlでもいけるはずだ。でも少しawkで頑張れば行けると思うけど面倒だから使わない。

$ curl -vu dharry: http://example.com 2>&1 | awk '{ if($0 ~/Authorization/) print $(NF)}'
ZGhhcnJ5Og==
$ echo -n ":" | base64
Og==
vbs

なんだかvbsが嫌になってきた。

Private Function base64(str)
	Dim dom, elem, bin, result

	Set st = CreateObject("ADODB.Stream")
	With st
		.Type = 2
		.Charset = "Shift-JIS"
		.Open
		.WriteText str
		.Position = 0
		.Type = 1
		bin = st.Read
		.Close
	End With

	Set dom = CreateObject("Microsoft.XMLDOM")
	Set elem = dom.CreateElement("tmp")
	elem.DataType = "bin.base64"
	elem.NodeTypedValue = bin
	result = elem.Text

	base64 = result
End Function
Wscript.echo base64("dharry")

秀丸ファイラーClassicがかなりイケてる

まじにWindows7のエクスプローラはうんざりです。秀丸ファイラーClassicは最高です。
そんな秀丸ファイラーClassicの [ヘルプ] -> [最新バージョンのチェック] をクリックしてみたらTwitterにアクセスすると言われました...。
f:id:dharry:20111013014836p:image


HmFilerClassicHmvc.vbs の 中を見てみたら、CreateObject("MSXML2.XMLHTTP")でrss取得する関数作って

Function GetHTTPFile(ByVal URL, ByRef ErrorMessage)
	'// 指定URLのファイルを取得します。

	Dim objMSXML, strRes

	ErrorMessage = ""
	Set objMSXML = CreateObject("MSXML2.XMLHTTP")
	If objMSXML Is Nothing Then
		ErrorMessage = "MSXML2.XMLHTTPオブジェクトが作成できませんでした。"
		GetHTTPFile = ""
		Exit Function
	End If
	With objMSXML
		Call .Open("GET", URL, False)
		Call .Send
		GetHTTPFile = .ResponseText
	End With
	Set objMSXML = Nothing
End Function


こんな感じにtimelineを取得ですか。

mstrS = GetHTTPFile("http://twitter.com/statuses/user_timeline/271738927.rss", mstrErrorMessage)


twitterのtimelineを取得して最新バーションのチェックってすげーユニークな方法だなと思いました。*1
あとvbsでtwitterという発想もなかった。ほんと年々頭が硬くなっているこの頃です。

*1:もしかして私が知らないだけなのかな

vbsの配列を少しだけ使いやすくするために配列Classを作ってみた。

rubyperlを使った後にvbsを使うと面倒で仕方がない。
せめて配列ぐらいは、LLっぽくできないものかと思い少し試してみた。気のせいかもしれないがもしかしたらvbsもやればできる子かもしれないと思った。

できたClass。配列のように扱うけど中身はHash(Dictionary)。

Option Explicit

Class CustomArray

  Public x, items

  Sub Class_Initialize()
    Set items = CreateObject("Scripting.Dictionary")
    x = 0
  End Sub
  
  Sub Class_Terminate()
    items.removeall
  End Sub

  Public Function push(ByVal item)
    items.item(x) = item
    x = x + 1
  End Function

  Public Function pop()
    Dim temp
    temp = items(x)
    items.Remove (x)
    x = x - 1
    pop = temp
  End Function

  Public Function size()
    size = items.Count
  End Function

  Public Default Property Get item(byval idx)
    item = items(idx)
  End Property

  Public Property Let item(ByVal idx, ByVal value)
    dim i
    if isNumeric(idx) Then
      if x >= idx then
        items.item(idx) = value
      else
        Do While idx > x
           x = x + 1
           items.item(x) = ""
        Loop
        items.item(idx) = value
      end if
    End if
  End Property

  Public Property Set item(ByVal idx, ByVal value)
    items.item(idx) = value
  End Property
  
  Public Function sort()
    Dim i, j, temp
    For Each i In items
      For Each j In items
        If (items.item(i) <= items.item(j)) Then
          temp = items.item(i)
          items.item(i) = items.item(j)
          items.item(j) = temp
        End If
      Next
    Next
  End Function

  Public Function reverse()
    Dim i, j, temp
    For Each i In items
      For Each j In items
        If (items.item(j) <= items.item(i)) Then
          temp = items.item(j)
          items.item(j) = items.item(i)
          items.item(i) = temp
        End If
      Next
    Next
  End Function

  Public Function uniq()
    Dim i, j, temp, empflg
    empflg = 0 : j = 0
    Set temp = CreateObject("Scripting.Dictionary")
    For Each i In items.keys
      if items(i) = "" then
        empflg = 1
      else
        temp.item(items(i)) = i
      end if
    Next
    items.removeall
    if empflg = 0 then
      j = 0
    else
      j = 1
      items.item(0) = ""
    end if
    For Each i In temp.keys
      items.item(j) = i
      j = j + 1
    Next
    set temp = Nothing
  End Function

End Class

javascriptrubyぽく、pushやpopで入れたり出したり。

Dim arr, x
set arr = new CustomArray 

arr.push("aaa")
arr.push("bbb")
arr.push("aaa")
arr(1) = "xxx"
arr(3) = "yyy"
arr(5) = "yyy"
arr(7) = "aaa"

For x = 0 To arr.size -1
  WScript.echo x & " " & arr(x)
Next
'#=>0 aaa
'#=>1 xxx
'#=>2 aaa
'#=>3 yyy
'#=>4
'#=>5 yyy
'#=>6
'#=>7 aaa

WScript.echo arr.pop
'#=>aaa

sortはrubyでいう破壊的メソッド。

Dim arr, x
set arr = new CustomArray 

arr.push("aaa")
arr.push("ccc")
arr.push("bbb")
arr.push("aaa")

arr.sort
For x = 0 To arr.size -1
  WScript.echo x & " " & arr(x)
Next
'#=>0 aaa
'#=>1 aaa
'#=>2 bbb
'#=>3 ccc

uniqもrubyでいう破壊的メソッド。Hash(Dictionary)は便利ですね。

Dim arr, x
set arr = new CustomArray 

arr.push("aaa")
arr.push("ccc")
arr.push("bbb")
arr.push("aaa")

arr.uniq
For x = 0 To arr.size -1
  WScript.echo x & " " & arr(x)
Next
'#=>0 aaa
'#=>1 ccc
'#=>2 bbb