暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

base64编解码的实现(golang版本和C版本)

原创 Yongtao 2024-07-10
91

Golang的base64编解码实现

第一种:NoPadding的

package main import ( "encoding/base64" "fmt" ) func main() { data := "changeme" sEnc := base64.StdEncoding.WithPadding(base64.NoPadding).EncodeToString([]byte(data)) fmt.Println(sEnc) // Y2hhbmdlbWU sDec, _ := base64.StdEncoding.WithPadding(base64.NoPadding).DecodeString(sEnc) fmt.Println(string(sDec)) // changeme fmt.Println() }

第二种:StdPadding的

package main import ( "encoding/base64" "fmt" ) func main() { data := "changeme" sEnc := base64.StdEncoding.WithPadding(base64.StdPadding).EncodeToString([]byte(data)) fmt.Println(sEnc) // Y2hhbmdlbWU= sDec, _ := base64.StdEncoding.WithPadding(base64.StdPadding).DecodeString(sEnc) fmt.Println(string(sDec)) // changeme fmt.Println() }

默认模式base64.StdEncoding.EncodeToString([]byte(data))等价于上述第二种base64.StdEncoding.WithPadding(base64.StdPadding).EncodeToString([]byte(data))

const (
	StdPadding rune = '=' // Standard padding character
	NoPadding  rune = -1  // No padding
)

源码里可以看到,如果使用StdPadding模式,如果长度不足,则会填充=

C语言的base64编解码实现

postgres里有一个很经典的base64编解码的实现,在postgres/scr/common目录下,我们抄过来测试一下。

#include <stdio.h> #include <string.h> #include <stdlib.h> typedef signed char int8; /* == 8 bits */ typedef unsigned int uint32; /* == 32 bits */ static const char _base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const int8 b64lookup[128] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, }; /* * pg_b64_encode * * Encode into base64 the given string. Returns the length of the encoded * string. */ int pg_b64_encode(const char *src, int len, char *dst) { char *p; const char *s, *end = src + len; int pos = 2; uint32 buf = 0; s = src; p = dst; while (s < end) { buf |= (unsigned char) *s << (pos << 3); pos--; s++; /* write it out */ if (pos < 0) { *p++ = _base64[(buf >> 18) & 0x3f]; *p++ = _base64[(buf >> 12) & 0x3f]; *p++ = _base64[(buf >> 6) & 0x3f]; *p++ = _base64[buf & 0x3f]; pos = 2; buf = 0; } } if (pos != 2) { *p++ = _base64[(buf >> 18) & 0x3f]; *p++ = _base64[(buf >> 12) & 0x3f]; *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '='; *p++ = '='; } return p - dst; } /* * pg_b64_decode * * Decode the given base64 string. Returns the length of the decoded * string on success, and -1 in the event of an error. */ int pg_b64_decode(const char *src, int len, char *dst) { const char *srcend = src + len, *s = src; char *p = dst; char c; int b = 0; uint32 buf = 0; int pos = 0, end = 0; while (s < srcend) { c = *s++; /* Leave if a whitespace is found */ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return -1; if (c == '=') { /* end sequence */ if (!end) { if (pos == 2) end = 1; else if (pos == 3) end = 2; else { /* * Unexpected "=" character found while decoding base64 * sequence. */ return -1; } } b = 0; } else { b = -1; if (c > 0 && c < 127) b = b64lookup[(unsigned char) c]; if (b < 0) { /* invalid symbol found */ return -1; } } /* add it to buffer */ buf = (buf << 6) + b; pos++; if (pos == 4) { *p++ = (buf >> 16) & 255; if (end == 0 || end > 1) *p++ = (buf >> 8) & 255; if (end == 0 || end > 2) *p++ = buf & 255; buf = 0; pos = 0; } } if (pos != 0) { /* * base64 end sequence is invalid. Input data is missing padding, is * truncated or is otherwise corrupted. */ return -1; } return p - dst; } /* * pg_b64_enc_len * * Returns to caller the length of the string if it were encoded with * base64 based on the length provided by caller. This is useful to * estimate how large a buffer allocation needs to be done before doing * the actual encoding. */ int pg_b64_enc_len(int srclen) { /* 3 bytes will be converted to 4 */ return (srclen + 2) / 3 * 4; } /* * pg_b64_dec_len * * Returns to caller the length of the string if it were to be decoded * with base64, based on the length given by caller. This is useful to * estimate how large a buffer allocation needs to be done before doing * the actual decoding. */ int pg_b64_dec_len(int srclen) { return (srclen * 3) >> 2; }

测试一下这两个函数:

int main() { char *input = "changeme"; char *base64_res = (char *) malloc(pg_b64_enc_len(strlen(input)) + 1); pg_b64_encode(input, strlen(input), base64_res); printf("input = %s\n", input); // changeme printf("base64_res = %s\n", base64_res); // Y2hhbmdlbWU= char *base64_dec_res = (char *) malloc(pg_b64_dec_len(strlen(base64_res)) + 1); pg_b64_decode(base64_res, strlen(base64_res), base64_dec_res); printf("base64_dec_res = %s\n", base64_dec_res); // changeme return 0; }

可以看出postgres实现的是前文说的golang实现的StdPadding模式。这样C语言和golang语言的base64实现结果就对应上了。

参考博客:https://blog.csdn.net/qq_26093511/article/details/78836087

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论