Google身份验证服务端实现

2023-05-16

import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
/**
 * google身份验证器,java服务端实现
 */
public class GoogleAuthenticator {
 
	// 生成的key长度( Generate secret key length)
	public static final int SECRET_SIZE = 10;
 
	public static final String SEED = "g8GjEvTbW5oVSV7avL47357438reyhreyuryetredLDVKs2m0QN7vxRs2im5MDaNCWGmcD2rvcZx";
	// Java实现随机数算法
	public static final String RANDOM_NUMBER_ALGORITHM = "SHA1PRNG";
	// 最多可偏移的时间
	int window_size = 3; // default 3 - max 17
 
	/**
	 * set the windows size. This is an integer value representing the number of
	 * 30 second windows we allow The bigger the window, the more tolerant of
	 * clock skew we are.
	 * 
	 * @param s
	 *            window size - must be >=1 and <=17. Other values are ignored
	 */
	public void setWindowSize(int s) {
		if (s >= 1 && s <= 17)
			window_size = s;
	}
 
	/**
	 * Generate a random secret key. This must be saved by the server and
	 * associated with the users account to verify the code displayed by Google
	 * Authenticator. The user must register this secret on their device.
	 * 生成一个随机秘钥
	 * 
	 * @return secret key
	 */
	public static String generateSecretKey() {
		SecureRandom sr = null;
		try {
			sr = SecureRandom.getInstance(RANDOM_NUMBER_ALGORITHM);
			sr.setSeed(Base64.decodeBase64(SEED));
			byte[] buffer = sr.generateSeed(SECRET_SIZE);
			Base32 codec = new Base32();
			byte[] bEncodedKey = codec.encode(buffer);
			String encodedKey = new String(bEncodedKey);
			return encodedKey;
		} catch (NoSuchAlgorithmException e) {
			// should never occur... configuration error
		}
		return null;
	}
 
	/**
	 * Return a URL that generates and displays a QR barcode. The user scans
	 * this bar code with the Google Authenticator application on their
	 * smartphone to register the auth code. They can also manually enter the
	 * secret if desired
	 * 
	 * @param user
	 *            user id (e.g. fflinstone)
	 * @param host
	 *            host or system that the code is for (e.g. myapp.com)
	 * @param secret
	 *            the secret that was previously generated for this user
	 * @return the URL for the QR code to scan
	 */
	public static String getQRBarcodeURL(String user, String host, String secret) {
		String format = "http://www.google.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth://totp/%s@%s?secret=%s";
		return String.format(format, user, host, secret);
	}
 
	/**
	 * 生成一个google身份验证器,识别的字符串,只需要把该方法返回值生成二维码扫描就可以了。
	 * 
	 * @param user
	 *            账号
	 * @param secret
	 *            密钥
	 * @return
	 */
	public static String getQRBarcode(String user, String secret) {
		String format = "otpauth://totp/%s?secret=%s";
		return String.format(format, user, secret);
	}
 
	/**
	 * Check the code entered by the user to see if it is valid 验证code是否合法
	 * 
	 * @param secret
	 *            The users secret.
	 * @param code
	 *            The code displayed on the users device
	 * @param timeMsec
	 *            The time in msec (System.currentTimeMillis() for example)
	 * @return
	 */
	public boolean check_code(String secret, long code, long timeMsec) {
		Base32 codec = new Base32();
		byte[] decodedKey = codec.decode(secret);
		// convert unix msec time into a 30 second "window"
		// this is per the TOTP spec (see the RFC for details)
		long t = (timeMsec / 1000L) / 30L;
		// Window is used to check codes generated in the near past.
		// You can use this value to tune how far you're willing to go.
		for (int i = -window_size; i <= window_size; ++i) {
			long hash;
			try {
				hash = verify_code(decodedKey, t + i);
			} catch (Exception e) {
				// Yes, this is bad form - but
				// the exceptions thrown would be rare and a static
				// configuration problem
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
				// return false;
			}
			if (hash == code) {
				return true;
			}
		}
		// The validation code is invalid.
		return false;
	}
 
	private static int verify_code(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException {
		byte[] data = new byte[8];
		long value = t;
		for (int i = 8; i-- > 0; value >>>= 8) {
			data[i] = (byte) value;
		}
		SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");
		Mac mac = Mac.getInstance("HmacSHA1");
		mac.init(signKey);
		byte[] hash = mac.doFinal(data);
		int offset = hash[20 - 1] & 0xF;
		// We're using a long because Java hasn't got unsigned int.
		long truncatedHash = 0;
		for (int i = 0; i < 4; ++i) {
			truncatedHash <<= 8;
			// We are dealing with signed bytes:
			// we just keep the first byte.
			truncatedHash |= (hash[offset + i] & 0xFF);
		}
		truncatedHash &= 0x7FFFFFFF;
		truncatedHash %= 1000000;
		return (int) truncatedHash;
	}
}

测试类实现

import org.junit.Test;
import java.util.HashSet;
import java.util.Set;

public class AuthTest {
	//当测试authTest时候,把genSecretTest生成的secret值赋值给它
		private static String secret="BYJBZYGU2AO4ZKK7";
	 
		@Test
		public void genSecretTest() {// 生成密钥
			secret = GoogleAuthenticator.generateSecretKey();
			// 把这个qrcode生成二维码,用google身份验证器扫描二维码就能添加成功
			String qrcode = GoogleAuthenticator.getQRBarcode("2816661736@qq.com", secret);
			System.out.println("qrcode:" + qrcode + ",key:" + secret);
		}
		/**
		 * 对app的随机生成的code,输入并验证
		 */
		 @Test
		public void verifyTest() {
		 	long code = 936171;
			long t = System.currentTimeMillis();
			GoogleAuthenticator ga = new GoogleAuthenticator();
			ga.setWindowSize(2); 
			boolean r = ga.check_code(secret, code, t);
			System.out.println("检查code是否正确?" + r);
		}

}

运行下面链接中下载的demo中的AuthTest的genSecretTest方法,控制台打印的结果如下图:
在这里插入图片描述
在这里插入图片描述
key:为app与服务端约定的秘钥,用于双方的认证。

qrcode:是app扫码能够识别的就是二维码值,把它生成二维码如下图:
在这里插入图片描述
打开google authenticator app软件选择扫描条形码按扭打开相机对二维码扫描加入账号,如下图:
在这里插入图片描述
把app中的数字,在AuthTest的verifyTest进行验证,如下图:
在这里插入图片描述
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google身份验证服务端实现 的相关文章

  • gtest学习笔记(二)gtest自带的sample1---Func函数使用

    文章目录 前言 源码学习 utest语法 运行 前言 第一章中已经编译出自带的sample例子 在build googletest目录下可以看到sample的各种例子的可执行程序 Google Test 附带了10个单元测试用例 难度由浅及
  • 谷歌免费域名邮箱申请全解

    谷歌google免费域名 企业 邮箱申请地址为 http www google com a cpanel domain new hl zh CN 点击进入填上姓名和邮箱等相关信息 国家选中国行不通 所以我选中国香港 电话号码要注意 我几次都
  • 质疑百度的“国学”

    2006年11月03日 18 07 00 百度 这个名字取得非常好 他有一个广告 也不错 这些无不在向人们展示 汉语 的魅力和中国文化的底蕴 才可能构成今日 世界有Google 中国有百度 的格局 但是从百度其中一个产品 国学 来看 百度做
  • java.lang.NoClassDefFoundError:com/google/common/base/Moreobjects

    项目所属环境不同 解决方案不一定适合所有人 见谅 java lang NoClassDefFoundError com google common base Moreobjects 编译可以通过 运行报错 很无奈 突然蹦出这鬼东西 经过一番
  • Google也裁员啦!!

    国外媒体报道 在谷歌 要不就不下雨 要下就是倾盆大雨 google宣布首次裁员 裁减内部员工100个 合同工和劳务工都将裁掉 挪威 瑞典 奥斯汀 得克萨斯 等分部将全部关闭 分部员工全部回美国总部 谷歌还发表数篇博客 详细说明了即将关闭的多
  • 百度tangram框架开发工具小结

    根据在开发流程中出现的顺序 对每个工具简要介绍如下 firebug 几乎所有写html css js代码的人都熟悉的工具 tangram开发调试中用到 官方站点 http getfirebug com closure linter gjsl
  • google Guava之EventBus

    文章目录 EventBus基本用法 1 创建Listener 2 创建EventBus并发送消息 Listener之间的继承关系 Subscriber 不同类型参数的Subscribe event 继承关系的event DeadEvent
  • eBay架构的思想金矿

    2008年01月24日 星期四 11 53 P M 英文来源 http www manageability org blog stuff about ebays architecture An accurate way of knowing
  • Code For Better 谷歌开发者之声 ——Tensorflow与深度学习

    给大家推荐一款丧心病狂的API测试工具 Apifox Apifox 是接口管理 开发 测试全流程集成工具 定位 Postman Swagger Mock JMeter 点击此处跳转体验 目录 一 TensorFlow简介 二 机器学习与深度
  • 巴比特

    摘要 元宇宙变得越来越重要 因为它为企业提供了一种与来自世界各地的用户进行交流和协作的新途径 从小企业到大公司 每个品牌都可以踏入虚拟世界 并从中获益 那么一般的企业如何将业务转移到元宇宙呢 这7个步骤了解一下 热点资讯 Meta 宣布大幅
  • 大话西游灯谜答案

  • 长尾理论(The Long Tail)与Google的成功

    根据wikipedia的解释 长尾 Long Tail 是2004年Chris Anderson在给连线杂志的文章中首次使用的词汇 用以描述某种经济模式如Amazon com或Netflix 长尾术语也普遍使用于统计学中 如 对财富分布或词
  • protobuf在C#项目中的使用

    protobuf在C 项目中的使用 在C 项目中 有时候会使用到使用到protobuf来作为通信时数据交换的格式 protobuf ProtocolBuffer 简称PB 是google 的一种数据交换的格式 这是一种二进制的格式 比使用x
  • 各种虚拟机体验杂谈 --- 兼发布 google chrome os (chromiumos) vmware版本

    前两天赶时髦 把笔记本换上了win8 pro 换win8pro的原因 一个是价格真的很有诚意 另一个就是从DP版本开始就一直用 虽然兼容性问题多多 但作为宿主主机还行 而且xenclient也实在是让人窝火 号称裸机虚拟 其实硬盘速度慢如蜗
  • libspark,不懂日文怎么学……

    由于工作的需求 还有我未来的规划 决心研究 AR 这必然要研究 FLARToolKit 于是我找到了 libspark 接着我发现 这里面的东西太多了太强大了 而且是日本人的开源项目 所以里面很多源码注释都是日文的 于是我今天申请了goog
  • 申请Google Player帐号上传自己开发的App

    1 访问https play google com apps publish signup 2 输入个人信息 3 在选择国家 地区时 由于列表中没有中国 所以我们只能选择香港 注册Google Player开发帐号是需要支付25美元费用的
  • 信息收集 (一)Google Hack & robots文件

    一 Google Hack 在渗透测试中 信息收集是尤为重要的一部分 甚至可以占到整个渗透的百分之六十至七十 可见掌握好信息收集的方法十分重要 那GoogleHacking作为常用且方便的信息收集搜索引擎工具 它是利用谷歌搜索强大 可以搜出
  • 太不可思议了,我的文章居然有人转载

    今天无意间逛Google 发现有人转载我的垃圾文章 简直不可思议 http www newbooks com cn info 50429 html 本来是写起耍的 算是整理加一点实际经验写成的 让我又高兴又惭愧 惭愧文章写的差 高兴我的文章
  • 众多Android 开源项目推荐,给力工作给力学习

    FBReaderJ FBReaderJ用于Android平台的电子书阅读器 它支持多种电子书籍格式包括 oeb ePub和fb2 此外还支持直接读取zip tar和gzip等压缩文档 项目地址 http www fbreader org F
  • 安装DirectXSDK时提示Error Code s1023

    向开发DirectX 3D游戏 就安装DirectX最新版 安装时遇到Error Code s1023 试了好多办法都不行 关掉所有程序还是不行 感谢伟大的Google在一个英文网站上提到如果安装了Microsoft Visual C 20

随机推荐

  • Http权威指南笔记(十三)-国际化

    HTTP报文可以承载任何语言表示的内容的 因为对HTTP来说 xff0c 实体主体真实二进制信息的容器而已 在HTTP中为了支持国际性 xff0c 服务器返回内容的同时需要告知客户端文档是用的什么字母表和语言等信息 xff0c 这样客户端才
  • Http权威指南笔记(十四)-内容协商与转码

    现在很多国际化的一些Web服务都会根据不同地区使用的语言不同 xff0c 返回不同语言的页面内容展示给用户 而这里面就涉及到本篇介绍的内容 内容协商与转码 1 内容协商的技术 目前的内容协商技术主要有3种 客户端驱动协商 服务器驱动协商和透
  • php curl 分离header和body信息

    php curl 分离header和body信息 php中可以通过curl来模拟http请求 xff0c 同时可以获取http response header和body xff0c 当然也设置参数可以只获取其中的某一个 当设置同时获取res
  • 文件缓冲区

    系统自动在内存区为程序中每一个正在使用的文件开辟一个文件缓冲区从内存向磁盘输出数据 xff0c 必须先送到内存中的缓冲区 xff0c 装满缓冲区后才一起送到磁盘 如果从磁盘向计算机读入数据 xff0c 则一次从磁盘文件将一批数据输入到内存缓
  • 【UE4学习】5.相机和蓝图进阶

    文章目录 相机基础Project Setting控制输入按键事件控制相机设置追踪目标CameraManager实现相机切换API接口与多态蓝图之间的通信方式GameMode 43 Manager显示当前相机信息事件调度器Sequencer入
  • 动态绑定实现的原理

    当用virtual关键字来声明一个成员函数 xff0c 编译器机会根据动态绑定机制在幕后完成一些工作 当编译器发现类中有虚函数的时候 xff0c 编译器会创建一张虚函数表 xff0c 把虚函数的函数入口地址放到虚函数表中 xff0c 并且在
  • 模板函数实现数组排序

    template lt class T gt void sortfun T arr int len int i j T tmp for i 61 0 i lt len 1 i 43 43 for j 61 i j lt len 1 j 43
  • 静态转换和动态转换

    1 静态转换 静态转换用于 xff0c 普通数据类型间的转换 xff0c 具有继承关系的父子类指针或引用的转换 class Dad class Son public Dad class MyClass 基础类型转换 void test1 i
  • 文件的原子操作

    文件的原子操作是指一个操作一旦启动 xff0c 则无法能被破坏它的其它操作打断 1 写文件原子操作 无论是两个打开 xff0c 还是dup xff0c 同时操作一个文件都可能引起混乱 xff0c 解决这个问题的方法是 xff0c 可以通过O
  • 目录操作

    创建目录 xff1a int mkdir const char pathname mode t mode xff1b pathname xff0c 路径 xff1b mode xff0c 目录访问权限 xff1b 返回值 xff1a 成功
  • 【UE4学习】6.粒子系统

    文章目录 粒子系统常用参数Simple Sprite Burst EmitterEmitter SettingsEmitter SpawnEmitter UpdateParticle SpawnParticle UpdateAdd Even
  • java中Array/List/Map/Object与Json互相转换详解

    JSON JavaScript Object Notation xff1a 是一种轻量级的数据交换格式 一 JSON建构有两种结构 xff1a 对象和数组 1 对象 xff1a 对象在js中表示为 扩起来的内容 xff0c 数据结构为 ke
  • ZipInputStream解压远程文件报错,java.lang.IllegalArgumentException: MALFORMED[1]

    我遇到的问题是报的这个错java lang IllegalArgumentException MALFORMED 1 at java util zip ZipCoder toString ZipCoder java 65 不是 java l
  • OAuth2.0接百度平台进行授权

    百度开发文档 xff1a https openauth baidu com doc regdevelopers html 1 注册开发者账号并创建一个应用 2 创建应用后 xff0c 获取API Key和Secret Key 3 创建一个S
  • Spring 中最常用的 11 个扩展点

    1 自定义拦截器 spring mvc拦截器根spring拦截器相比 xff0c 它里面能够获取HttpServletRequest和HttpServletResponse等web对象实例 spring mvc拦截器的顶层接口是 xff1a
  • 经典排序算法

    https juejin cn post 7198840786766102589
  • SpringBoot项目启动加载时排除某一个类

    在Application启动类上 xff0c 用这个注解就可以指定某个类不加载进容器 64 ComponentScan
  • 手写一个生产者/消费者模式(三种方式实现)

    这种设计模式需要满足以下三点要求 xff1a xff08 1 xff09 生产者生产数据到缓冲区中 xff0c 消费者从缓冲区中取数据 xff08 2 xff09 如果缓冲区已经满了 xff0c 则生产者线程阻塞 xff1b xff08 3
  • Android中Okhttp,Volley,Retrofit网络框架优缺点及对比

    Okhttp xff1a Square 公司开源的 OkHttp 是一个专注于连接效率的 HTTP 客户端 OkHttp 提供了对 HTTP 2 和 SPDY 的支持 xff0c 并提供了连接池 xff0c GZIP 压缩和 HTTP 响应
  • Google身份验证服务端实现

    import org apache commons codec binary Base32 import org apache commons codec binary Base64 import javax crypto Mac impo