Skip to content

hiroraba/Perl6-Base64

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Base64

Lazy base64 encoding and decoding routines

SYNOPSIS

use Base64;


# ENCODING

> say encode-base64("g-g-g-g-g unit", :str);
Zy1nLWctZy1nIHVuaXQ=

> say encode-base64("g-g-g-g-g unit").perl;
("Z", "y", "1", "n", "L", "W", "c", "t", "Z", "y", "1", "n", "I", "H", "V", "u", "a", "X", "Q", "=").Seq

> .print for lazy encode-base64("g-g-g-g-g unit")
Zy1nLWctZy1nIHVuaXQ=


# DECODING

> say decode-base64("Zy1nLWctZy1nIHVuaXQ=", :buf).decode
g-g-g-g-g unit

> say decode-base64("Zy1nLWctZy1nIHVuaXQ=", :buf)
Buf:0x<67 2d 67 2d 67 2d 67 2d 67 20 75 6e 69 74>

> say decode-base64("Zy1nLWctZy1nIHVuaXQ=").perl
(103, 45, 103, 45, 103, 45, 103, 45, 103, 32, 117, 110, 105, 116).Seq

Exports

Args $encode-me where Blob|Str, :$pad, :@alpha, Bool :$str

Returns Seq|Str

encode-base64($encode-me)                  # Returns a base64 encoded string
encode-base64($encode-me, :str)            # Stringify and return the sequence that would be returned
encode-base64($encode-me, :!pad)           # No padding
encode-base64($encode-me, :pad("*"))       # Alternative padding character
encode-base64($encode-me, :uri)            # Use '-' and '_' for chars 63 and 64
encode-base64($encode-me, :alpha(1..64))   # Set the entire alphabet
encode-base64($encode-me, :alpha('-','_')) # Same as :uri

Takes a Blob and applies base64 encoding with the requested options. If passed a Str it will be converted to a Blob via .ords first.

> say encode-base64("test", :str)
dGVzdA==

> say encode-base64(Buf.new("test".ords), :str)
dGVzdA==

> say encode-base64("test").perl'
("d", "G", "V", "z", "d", "A", "=", "=").Seq

Args $decode-me where Blob|Str, :$pad, :@alpha, Bool :$buf

Returns Seq|Buf

decode-base64($decode-me)                  # Decodes a base64 encoded string to a Buf for further decoding
decode-base64($encode-me, :buf)            # Return a buffer from the sequence that would be returned
decode-base64($decode-me, :!pad)           # No padding
decode-base64($decode-me, :pad("*"))       # Alternative padding character
decode-base64($decode-me, :uri)            # Use '-' and '_' for chars 63 and 64
decode-base64($decode-me, :alpha(1..64))   # Set the entire alphabet
decode-base64($decode-me, :alpha('-','_')) # Same as :uri

Takes a Str and applies base64 decoding with the requested options. If passed a Blob it will be converted to a Str with .decode first.

> say decode-base64("YW55IGNhcm5hbCBwbGVhc3VyZS4=", :buf).decode
any carnal pleasure.

> say Buf.new(decode-base64("YW55IGNhcm5hbCBwbGVhc3VyZS4=")).decode('utf-8')
any carnal pleasure.

> say decode-base64("YW55IGNhcm5hbCBwbGVhc3VyZS4=").perl
(97, 110, 121, 32, 99, 97, 114, 110, 97, 108, 32, 112, 108, 101, 97, 115, 117, 114, 101, 46).Seq

TODO

  • Handle line lengths, per requested, transparently for the user

About

Lazy base64 encoding and decoding routines for Perl6

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Other 100.0%