推广 热搜:   公司  企业  中国  快速    行业  上海  未来  设备 

google身份验证器C语言接口

   日期:2024-12-27     移动:http://fabua.ksxb.net/mobile/quote/4779.html
#include <stdlib.h>

google身份验证器C语言接口

#include <stdio.h> #include <assert.h> #include <stdint.h> #include <time.h> #include "ga.h" #pragma warning(disable:4996) // typedef unsigned int size_t; // typedef int ptrdiff_t; // typedef int intptr_t; static const size_t base32_ENCODE_INPUT = 5; static const size_t base32_ENCODE_OUTPUT = 8; static const char* const base32_ENCODE_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567="; //返回输出缓冲区需要的大小 size_t base32EncodeGetLength(size_t size) { return (((size + base32_ENCODE_INPUT - 1) / base32_ENCODE_INPUT) * base32_ENCODE_OUTPUT) + 1; } //base32编码,size是src字符串的长度不包括结尾的'0' size_t base32Encode(char* dest, const void* src, size_t size) { if (dest && src) { unsigned char* pSrc = (unsigned char*)src; size_t dwSrcSize = size; size_t dwDestSize = 0; size_t dwBlockSize; unsigned char n1, n2, n3, n4, n5, n6, n7, n8; while (dwSrcSize >= 1) { dwBlockSize = (dwSrcSize < base32_ENCODE_INPUT ? dwSrcSize : base32_ENCODE_INPUT); n1 = n2 = n3 = n4 = n5 = n6 = n7 = n8 = 0; switch (dwBlockSize) { case 5: n8 = (pSrc[4] & 0x1f); n7 = ((unsigned char)(pSrc[4] & 0xe0) >> 5); case 4: n7 |= ((unsigned char)(pSrc[3] & 0x03) << 3); n6 = ((unsigned char)(pSrc[3] & 0x7c) >> 2); n5 = ((unsigned char)(pSrc[3] & 0x80) >> 7); case 3: n5 |= ((unsigned char)(pSrc[2] & 0x0f) << 1); n4 = ((unsigned char)(pSrc[2] & 0xf0) >> 4); case 2: n4 |= ((unsigned char)(pSrc[1] & 0x01) << 4); n3 = ((unsigned char)(pSrc[1] & 0x3e) >> 1); n2 = ((unsigned char)(pSrc[1] & 0xc0) >> 6); case 1: n2 |= ((unsigned char)(pSrc[0] & 0x07) << 2); n1 = ((unsigned char)(pSrc[0] & 0xf8) >> 3); break; default: assert(0); } pSrc += dwBlockSize; dwSrcSize -= dwBlockSize; assert(n1 <= 31); assert(n2 <= 31); assert(n3 <= 31); assert(n4 <= 31); assert(n5 <= 31); assert(n6 <= 31); assert(n7 <= 31); assert(n8 <= 31); switch (dwBlockSize) { case 1: n3 = n4 = 32; case 2: n5 = 32; case 3: n6 = n7 = 32; case 4: n8 = 32; case 5: break; default: assert(0); } *dest++ = base32_ENCODE_TABLE[n1]; *dest++ = base32_ENCODE_TABLE[n2]; *dest++ = base32_ENCODE_TABLE[n3]; *dest++ = base32_ENCODE_TABLE[n4]; *dest++ = base32_ENCODE_TABLE[n5]; *dest++ = base32_ENCODE_TABLE[n6]; *dest++ = base32_ENCODE_TABLE[n7]; *dest++ = base32_ENCODE_TABLE[n8]; dwDestSize += base32_ENCODE_OUTPUT; } *dest++ = 'x0'; return dwDestSize; } else return 0; } static const size_t base32_DECODE_INPUT = 8; static const size_t base32_DECODE_OUTPUT = 5; static const size_t base32_DECODE_MAX_PADDING = 6; static const unsigned char base32_DECODE_MAX = 31; static const unsigned char base32_DECODE_TABLE[0x80] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x20, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; int cyobase32Validate(const char* src, size_t size) { if (size % base32_DECODE_INPUT != 0) return -1; for (; size >= 1; --size, ++src) { unsigned char ch = *src; if ((ch >= 0x80) || (base32_DECODE_TABLE[ch] > base32_DECODE_MAX)) break; } for (; 1 <= size && size <= base32_DECODE_MAX_PADDING; --size, ++src) { unsigned char ch = *src; if ((ch >= 0x80) || (base32_DECODE_TABLE[ch] != base32_DECODE_MAX + 1)) break; } if (size != 0) return -2; return 0; } size_t base32DecodeGetLength(size_t size) { if (size % base32_DECODE_INPUT == 0) return (((size + base32_DECODE_INPUT - 1) / base32_DECODE_INPUT) * base32_DECODE_OUTPUT) + 1; else return 0; } size_t base32Decode(void* dest, const char* src, size_t size) { if (dest && src && (size % base32_DECODE_INPUT == 0)) { unsigned char* pDest = (unsigned char*)dest; size_t dwSrcSize = size; size_t dwDestSize = 0; unsigned char in1, in2, in3, in4, in5, in6, in7, in8; while (dwSrcSize >= 1) { in1 = *src++; in2 = *src++; in3 = *src++; in4 = *src++; in5 = *src++; in6 = *src++; in7 = *src++; in8 = *src++; dwSrcSize -= base32_DECODE_INPUT; if (in1 >= 0x80 || in2 >= 0x80 || in3 >= 0x80 || in4 >= 0x80 || in5 >= 0x80 || in6 >= 0x80 || in7 >= 0x80 || in8 >= 0x80) return 0; in1 = base32_DECODE_TABLE[in1]; in2 = base32_DECODE_TABLE[in2]; in3 = base32_DECODE_TABLE[in3]; in4 = base32_DECODE_TABLE[in4]; in5 = base32_DECODE_TABLE[in5]; in6 = base32_DECODE_TABLE[in6]; in7 = base32_DECODE_TABLE[in7]; in8 = base32_DECODE_TABLE[in8]; if (in1 > base32_DECODE_MAX || in2 > base32_DECODE_MAX) return 0; if ((int)in3 > (int)base32_DECODE_MAX + 1 || (int)in4 > (int)base32_DECODE_MAX + 1 || (int)in5 > (int)base32_DECODE_MAX + 1 || (int)in6 > (int)base32_DECODE_MAX + 1 || (int)in7 > (int)base32_DECODE_MAX + 1 || (int)in8 > (int)base32_DECODE_MAX + 1) return 0; *pDest++ = ((unsigned char)(in1 & 0x1f) << 3) | ((unsigned char)(in2 & 0x1c) >> 2); *pDest++ = ((unsigned char)(in2 & 0x03) << 6) | ((unsigned char)(in3 & 0x1f) << 1) | ((unsigned char)(in4 & 0x10) >> 4); *pDest++ = ((unsigned char)(in4 & 0x0f) << 4) | ((unsigned char)(in5 & 0x1e) >> 1);
本文地址:http://fabua.ksxb.net/quote/4779.html    海之东岸资讯 http://fabua.ksxb.net/ , 查看更多

特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


相关最新动态
推荐最新动态
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  粤ICP备2023022329号