📄 snmputilities.java
字号:
// NAME// $RCSfile: SnmpUtilities.java,v $// DESCRIPTION// [given below in javadoc format]// DELTA// $Revision: 1.24 $// CREATED// $Date: 2006/03/23 14:54:10 $// COPYRIGHT// Westhawk Ltd// TO DO///* * Copyright (C) 2000 - 2006 by Westhawk Ltd * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a> * * Permission to use, copy, modify, and distribute this software * for any purpose and without fee is hereby granted, provided * that the above copyright notices appear in all copies and that * both the copyright notice and this permission notice appear in * supporting documentation. * This software is provided "as is" without express or implied * warranty. * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> */package uk.co.westhawk.snmp.util;import java.io.*;import java.util.*;import uk.co.westhawk.snmp.stack.*;import org.bouncycastle.crypto.*;import org.bouncycastle.crypto.digests.*;import org.bouncycastle.crypto.params.*;import org.bouncycastle.crypto.engines.*;/** * This class contains utilities for key and authentication encoding. * See <a href="http://www.ietf.org/rfc/rfc3414.txt">SNMP-USER-BASED-SM-MIB</a>. * * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> * @version $Revision: 1.24 $ $Date: 2006/03/23 14:54:10 $ */public class SnmpUtilities extends Object{ private static final String version_id = "@(#)$Id: SnmpUtilities.java,v 1.24 2006/03/23 14:54:10 birgit Exp $ Copyright Westhawk Ltd"; final static int ONEMEG = 1048576; final static int SALT_LENGTH = 8; // in bytes private static int salt_count = -1;/** * Returns the String representation of the SNMP version number. * @param version The version number * @return The corresponding String. */public static String getSnmpVersionString(int version){ String versionString; switch (version) { case SnmpConstants.SNMP_VERSION_1: versionString = "SNMPv1"; break; case SnmpConstants.SNMP_VERSION_2c: versionString = "SNMPv2c"; break; case SnmpConstants.SNMP_VERSION_3: versionString = "SNMPv3"; break; default: versionString = "Unsupported version no " + version; } return versionString;}/** * Converts a hexadecimal ASCII string to a byte array. The method is case * insensitive, so F7 works as well as f7. The string should have * the form F7d820 and should omit the '0x'. * This method is the reverse of <code>toHexString</code>. * * @param hexStr The string representing a hexadecimal number * @return the byte array of hexStr * @see #toHexString(byte[]) */public static byte [] toBytes(String hexStr){ byte mask = (byte) 0x7F; byte [] bytes = new byte[0]; if (hexStr != null) { hexStr = hexStr.toUpperCase(); int len = hexStr.length(); bytes = new byte[(len/2)]; int sPos=0; // position in hexStr int bPos=0; // position in bytes while (sPos<len) { char a = hexStr.charAt(sPos); char b = hexStr.charAt(sPos+1); int v1 = Character.digit(a, 16); int v2 = Character.digit(b, 16); int v3 = (int) (v1 * 16 + v2); bytes[bPos] = (byte) v3; sPos +=2; bPos++; } } return bytes;}/** * Converts one long value to its byte value. * * @param l The long value * @return It's byte value * @throws IllegalArgumentException when l is not in between 0 and 255. * * @see #longToByte(long[]) * @since 4_14 */public static byte longToByte(long l) throws IllegalArgumentException{ byte ret = 0; if ((l < 0) || (l > 255)) { throw new IllegalArgumentException("Valid byte values are between 0 and 255." + "Got " + l); } ret = (byte)(l); return ret;}/** * Converts an array of long values to its array of byte values. * * @param l The array of longs * @return The array of bytes * @throws IllegalArgumentException when one of the longs is not in between 0 and 255. * * @see #longToByte(long) * @since 4_14 */public static byte[] longToByte(long[] l) throws IllegalArgumentException{ int len = l.length; byte [] ret = new byte[len]; for (int i=0; i<len; i++) { ret[i] = longToByte(l[i]); } return ret;}/** * Dumps (prints) the byte array. Debug method. * @param headerStr String that will be printed as header * @param bytes Bytes to be dumped as hex. */public static void dumpBytes(String headerStr, byte[] bytes){ StringBuffer buf = new StringBuffer(bytes.length); buf.append("\n"); buf.append(headerStr).append("\n"); buf.append("bytes.length: ").append(bytes.length).append("\n"); int len = bytes.length; int i=0; for (i=0; i<len; i++) { buf.append(toHex(bytes[i]) + " "); if (0 == ((i+1) % 8)) { buf.append("\n"); } } buf.append("\n"); System.out.println(buf.toString());}/** * Converts a byte array to a hexadecimal ASCII string. * The string will be in upper case and does not start with '0x'. * This method is the reverse of <code>toBytes</code>. * * @param bytes The byte array * @return The string representing the byte array * @see #toBytes(String) */public static String toHexString(byte[] bytes){ String str = ""; if (bytes != null) { int len = bytes.length; for (int i=0; i<len; i++) { str += toHex(bytes[i]); } } return str;}/** * Converts one int to a hexadecimal ASCII string. * * @param val The integer * @return The hex string */public static String toHex(int val){ int val1, val2; val1 = (val >> 4) & 0x0F; val2 = (val & 0x0F); return ("" + HEX_DIGIT[val1] + HEX_DIGIT[val2]);}final static char[] HEX_DIGIT = {'0','1','2','3','4','5','6','7', '8','9','A','B','C','D','E','F'};/** * Compaires two byte arrays and returns if they are equal. * * @param array1 the first byte array * @param array2 the second byte array * @return whether they are equal of not. */public static boolean areBytesEqual(byte[] array1, byte[] array2){ boolean same = true; int len1 = array1.length; if (len1 == array2.length) { int i=0; while (i<len1 && same) { same = (array1[i] == array2[i]); i++; } } else { same = false; } return same;}/** * Converts the user's password and the SNMP Engine Id to the localized key * using the MD5 protocol. * Described in <a href="http://www.ietf.org/rfc/rfc3414.txt">SNMP-USER-BASED-SM-MIB</a>. * * @param passwKey The password key * @param engineId The SNMP engine Id * @see uk.co.westhawk.snmp.stack.SnmpContextv3#setUserAuthenticationPassword(String) * @see #passwordToKeyMD5(String) */public static byte [] getLocalizedKeyMD5(byte[] passwKey, String engineId){ byte [] ret = null; MD5Digest mdc = new MD5Digest(); mdc.reset(); byte [] beid = toBytes(engineId); if ((beid != null) && (passwKey != null)) { // see page 169 of 0-13-021453-1 A Practical Guide to SNMP mdc.update(passwKey, 0, passwKey.length); mdc.update(beid, 0, beid.length); mdc.update(passwKey, 0, passwKey.length); ret = new byte[mdc.getDigestSize()]; mdc.doFinal(ret, 0); } return ret;}/** * Converts the user's password and the SNMP Engine Id to the localized key * using the SHA protocol. * * @param password The printable user password * @param engineId The SNMP engine Id * @see uk.co.westhawk.snmp.stack.SnmpContextv3#setUserAuthenticationPassword(String) */public static byte [] getLocalizedKeySHA1(byte[] passwKey, String engineId){ byte [] ret = null; SHA1Digest mdc = new SHA1Digest(); mdc.reset(); byte [] beid = toBytes(engineId); if ((beid != null) && (passwKey != null)) { // see page 169 of 0-13-021453-1 A Practical Guide to SNMP mdc.update(passwKey, 0, passwKey.length); mdc.update(beid, 0, beid.length); mdc.update(passwKey, 0, passwKey.length); ret = new byte[mdc.getDigestSize()]; mdc.doFinal(ret, 0); } return ret;}/** * Converts the user's password to an authentication key using the SHA1 * protocol. Note, this is not the same as generating the localized key * as is * described in <a href="http://www.ietf.org/rfc/rfc3414.txt">SNMP-USER-BASED-SM-MIB</a>. * * @param password The printable user password * @see uk.co.westhawk.snmp.stack.SnmpContextv3#setUserAuthenticationPassword(String) * @see #getLocalizedKeyMD5 */public static byte [] passwordToKeySHA1(String password){ SHA1Digest sha; byte [] ret =null; sha = new SHA1Digest(); byte [] passwordBuf = new byte[64]; int pl = password.length(); byte [] pass = new byte[pl]; // copy to byte array - stripping off top byte for (int i=0;i<pl; i++) { pass[i] = (byte) (0xFF & password.charAt(i)); } int count=0; int passwordIndex = 0; Date then = (AsnObject.debug > 1) ? new Date() : null ; synchronized (sha) { while (count < ONEMEG) { int cp = 0; int i=0; while (i<64) { int pim = passwordIndex % pl; int len = 64 - cp ; int pr = pl - pim; if (len > pr) { len = pr; } System.arraycopy(pass, pim, passwordBuf, cp, len); i+= len; cp+=len; passwordIndex += len; } // need to optimize this..... sha.update(passwordBuf, 0, passwordBuf.length); count += 64; } // implicit that ONEMEG % 64 == 0 ret = new byte[sha.getDigestSize()]; sha.doFinal(ret, 0); } if (AsnObject.debug > 1) { Date now = new Date(); long diff = now.getTime() - then.getTime(); System.out.println("(Complex) pass to key takes " + diff/1000.0); } return ret;}/** * Converts the user's password to an authentication key using the MD5 * protocol. Note, this is not the same as generating the localized key * as is * described in <a href="http://www.ietf.org/rfc/rfc3414.txt">SNMP-USER-BASED-SM-MIB</a>. * * @param password The printable user password * @see uk.co.westhawk.snmp.stack.SnmpContextv3#setUserAuthenticationPassword(String) * @see #getLocalizedKeyMD5
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -