当前位置: 首页 > news >正文

MessageAuthenticator

MessageAuthenticator

https://coova.github.io/JRadius/
https://coova.github.io/JRadius/

import org.tinyradius.packet.RadiusPacket;
import org.tinyradius.util.RadiusUtil;
import java.nio.charset.StandardCharsets;

public class RadiusAuthUtils {

    /**
     * 生成 RADIUS 报文的 Message-Authenticator 值
     * @param sharedSecret 共享密钥
     * @param requestAuthenticator 请求认证器(16 字节随机数)
     * @param packetBytes 完整的 RADIUS 报文字节数据(包含报文头和属性)
     * @return Message-Authenticator 的字节数组(16 字节)
     */
    public static byte[] generateMessageAuthenticator(String sharedSecret, byte[] requestAuthenticator, byte[] packetBytes) {
        try {
            // 1. 使用 HMAC-MD5 计算哈希
            byte[] hmacMd5 = RadiusUtil.getMd5Digest(sharedSecret.getBytes(StandardCharsets.UTF_8), packetBytes);

            // 2. 提取前 16 字节作为 Message-Authenticator
            byte[] messageAuthenticator = new byte[16];
            System.arraycopy(hmacMd5, 0, messageAuthenticator, 0, 16);
            return messageAuthenticator;
        } catch (Exception e) {
            throw new RuntimeException("生成 Message-Authenticator 失败", e);
        }
    }
}
public static byte[] generateMessageAuthenticator(byte[] sharedSecret, RADIUS packet) {
    try {
        byte[] messageBytes = packet.getBytes(); 
        SecretKeySpec secretKeySpec = new SecretKeySpec(sharedSecret, "HmacMD5");
        Mac mac = Mac.getInstance("HmacMD5"); 
        mac.init(secretKeySpec); 
        byte[] macValue = mac.doFinal(messageBytes); 
        
        Attribute macAttribute = new MessageAuthenticator(macValue);
        List<Attribute> attributes = packet.getAttributes(); 
        attributes.add(macAttribute); 
        
        return macValue;
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to generate Message-Authenticator", e);
    }
}

jradius-1.1.5

package net.jradius.util;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.io.IOException;
import java.nio.ByteBuffer;

import net.jradius.packet.RadiusFormat;
import net.jradius.packet.RadiusPacket;
import net.jradius.packet.attribute.AttributeDictionary;
import net.jradius.packet.attribute.AttributeFactory;
import net.jradius.packet.attribute.RadiusAttribute;

public class MessageAuthenticator 
{
    private static final RadiusFormat format = RadiusFormat.getInstance();

    public static void generateRequestMessageAuthenticator(RadiusPacket request, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException
    {
        byte[] hash = new byte[16];
        ByteBuffer buffer = ByteBuffer.allocate(4096);
        request.overwriteAttribute(AttributeFactory.newAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR, hash, request.isRecyclable()));
        format.packPacket(request, sharedSecret, buffer, true);
        System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);
	}
    
    public static void generateResponseMessageAuthenticator(RadiusPacket request, RadiusPacket reply, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException
    {
        byte[] hash = new byte[16];
        byte[] requestAuth = request.getAuthenticator();
        byte[] replyAuth = reply.getAuthenticator();
        ByteBuffer buffer = ByteBuffer.allocate(4096);
        reply.setAuthenticator(requestAuth);
        reply.overwriteAttribute(AttributeFactory.newAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR, hash, reply.isRecyclable()));
        format.packPacket(reply, sharedSecret, buffer, true);
        System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);
        reply.setAuthenticator(replyAuth);
	}
    
    public static Boolean verifyRequest(RadiusPacket request, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException
    {
        byte[] hash = new byte[16];
        ByteBuffer buffer = ByteBuffer.allocate(4096);

        RadiusAttribute attr = request.findAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR);
        if (attr == null) return null;
        
        byte[] pval = attr.getValue().getBytes();
        attr.setValue(hash);
        
        format.packPacket(request, sharedSecret, buffer, true);
        System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);

        attr.setValue(pval);
        
        return new Boolean(Arrays.equals(pval, hash));
    }

    public static Boolean verifyReply(byte[] requestAuth, RadiusPacket reply, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException
    {
        byte[] replyAuth = reply.getAuthenticator();
        byte[] hash = new byte[16];

        ByteBuffer buffer = ByteBuffer.allocate(4096);

        RadiusAttribute attr = reply.findAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR);
        if (attr == null) return null;
        
        byte[] pval = attr.getValue().getBytes();
        attr.setValue(hash);
        
        reply.setAuthenticator(requestAuth);

        format.packPacket(reply, sharedSecret, buffer, true);
        System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);

        reply.setAuthenticator(replyAuth);
        
        return new Boolean(Arrays.equals(pval, hash));
    }
}

相关文章:

  • 设计模式-(单例,简单工厂,工厂,抽象工厂)
  • 【组态PLC】基于三菱西门子S7-200PLC和组态王自动洗衣机组态设计【含PLC组态源码 M012期】
  • [创业之路-326]:两种事业部授权模式:战略管控与运营管控
  • 数位dp-
  • el-select滚动获取下拉数据;el-select滚动加载
  • 【信息系统项目管理师-案例真题】2011上半年案例分析答案和详解
  • 使用python接入腾讯云DeepSeek
  • 数据类型转换
  • 项目范围管理--从规划到控制项目范围的核心思想
  • 多弹协同末制导律设计
  • C++与Python实现LiDAR点云投影对比:关键差异与易错点详解
  • CAESAR II 14管道应力和柔性分析软件
  • 【WSL2】 Ubuntu20.04 GUI图形化界面 VcXsrv ROS noetic Vscode 配置
  • Scratch032(百发百中)
  • Kafka RecordTooLargeException问题解决
  • 第三章 语言基础
  • 2025年信息科学与工程学院科协机器学习介绍——机器学习基本模型介绍
  • 智慧后勤的消防管理:豪越科技为安全护航
  • TDengine数据订阅新手入门避坑指南1/3
  • 2025/2/17--2/23学习笔记(week1)_C语言
  • 以色列总理:以哈谈判内容包括“结束战争的框架”
  • 全国游泳冠军赛:孙杨、潘展乐同进400自决赛,今晚将正面对决
  • 穆迪下调美国主权信用评级
  • 我使馆就中国公民和企业遭不公正待遇向菲方持续提出严正交涉
  • 国税总局上海市税务局通报:收到王某对刘某某及相关企业涉税问题举报,正依法依规办理
  • 牛市早报|4月新增社融1.16万亿,降准今日正式落地