从shell编码为base32


9

我正在寻找直接从外壳将输入字符串编码为base32编码。我希望在ubuntu中做到这一点,但我想这里的味道并不是特别重要。

是否有任何现有的linux / unix工具可以简单地做到这一点?

类似于以下内容:

-bash-3.2$ echo -n 'hello' | base32

Answers:


10

嗯,快速的软件包搜索并没有像单个独立的实用程序那样的东西。

另一方面,它表明存在一个合适的Perl库,并且很容易生成快速的Perl脚本。就像是:

$ sudo apt-get install libmime-base32-perl

然后是一个脚本base32enc.pl

#!/usr/bin/perl

use MIME::Base32 qw( RFC );

undef $/;  # in case stdin has newlines
$string = <STDIN>;

$encoded = MIME::Base32::encode($string);

print "$encoded\n";

所以:

$ echo -n "hello" | ./base32enc.pl
NBSWY3DP

相对稀疏的CPAN条目是:http : //search.cpan.org/~danpeder/MIME-Base32-1.01/Base32.pm

因此,只需稍作更改,即可进行解码。


2

只是对cjc最佳答案的一种改进,因此我们可以使用与编码和解码方式base32类似的实用程序base64

#! /usr/bin/perl

use MIME::Base32;
use strict;

undef $/;

my $string = <STDIN>;
my $changed;

if ( $ARGV[0] eq "-d" ){
        $changed = MIME::Base32::decode($string);
}else{
        $changed = MIME::Base32::encode($string); 
}

if ( $changed =~ /\n$/ ) {
    printf $changed;
}else{
    printf $changed . "\n";
}

测试:

$ base32 < <(echo -n 'abcdef')
MFRGGZDFMY
$ base32 -d < <(echo  'MFRGGZDFMY')
abcdef


2

使用Python:

$ python
Python 2.7.14 (default, Sep 27 2017, 12:15:00) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> base64.b32encode('hello')
'NBSWY3DP'

0
  1. 安装perl-MIME-Base32.noarch

    yum install perl-MIME-Base32.noarch
    
  2. 将脚本保存在bas32文件名中:

    #!/usr/bin/perl
    
    use MIME::Base32 qw( RFC );
    
    undef $/;  # in case stdin has newlines
    $ed=$ARGV[0];
    $string=$ARGV[1];
    if ($ed eq "-e")
    {
    $encoded = MIME::Base32::encode($string);
    print "$encoded\n";
    }
    elsif ($ed eq "-d")
    {
    $decoded = MIME::Base32::decode($string);
    print "$decoded\n";
    }
    else { print " please pass option also\n";
    exit;
    }
    
    chmod +x base32
    cp base32 /usr/bin/
    base32 -e string
    base32 -d "any encoded value"
    
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.