git中ssh-keygen 中生成的 randomart image 用java的实现

1 篇文章 0 订阅

首先,先创建一个cube类,方便按路径移动和画图,直接贴码

package com.test.sshkeygen;

import static com.test.sshkeygen.Cube.CubeType.A;
import static com.test.sshkeygen.Cube.CubeType.MMM;

/**
 */
public class Cube {
    public  static enum  CubeType{
        MMM,LL,RR,TT,BB,A,B,C,D
    }
    public CubeType cubeType;

    public Cube() {

    }
    public int horizontalMax;
    public int verticalMax;

    public Cube(int horizontal, int vertical) {
        this.horizontal = horizontal;
        this.vertical = vertical;
    }

    public int getHorizontalMax() {
        return horizontalMax;
    }

    public void setHorizontalMax(int horizontalMax) {
        this.horizontalMax = horizontalMax;
    }

    public int getVerticalMax() {
        return verticalMax;
    }

    public void setVerticalMax(int verticalMax) {
        this.verticalMax = verticalMax;
    }

    public CubeType getCubeType() {
        return cubeType;
    }

    public void setCubeType(CubeType cubeType) {
        this.cubeType = cubeType;
    }

    private int horizontal;
    private int vertical;

    public int getHorizontal() {
        return horizontal;
    }

    public void setHorizontal(int horizontal) {
        this.horizontal = horizontal;
    }

    public int getVertical() {
        return vertical;
    }

    public void setVertical(int vertical) {
        this.vertical = vertical;
    }
    public void turnLT(){
        //↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖
        if (getCubeType()==CubeType.LL||getCubeType()==CubeType.C){
            this.vertical--;
        }else if (getCubeType()==CubeType.A){
            //无操作
        }else if (getCubeType()==CubeType.TT||getCubeType()==CubeType.B){
            this.horizontal--;
        }else{
            this.vertical--;
            this.horizontal--;
        }
        setCubeTypeAdvanced();
    }
    public void turnRT(){
        //↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗
        if (getCubeType()==CubeType.RR||getCubeType()==CubeType.D){
            this.vertical--;
        }else if (getCubeType()==CubeType.B){
            //无操作
        }else if (getCubeType()==CubeType.TT||getCubeType()==CubeType.B){
            this.horizontal++;
        }else{
            this.vertical--;
            this.horizontal++;
        }
        setCubeTypeAdvanced();
    }
    public void turnLB(){
        //↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙
        if (getCubeType()==CubeType.LL||getCubeType()==CubeType.A){
            this.vertical++;
        }else if (getCubeType()==CubeType.C){
            //无操作
        }else if (getCubeType()==CubeType.BB||getCubeType()==CubeType.D){
            this.horizontal--;
        }else{
            this.vertical++;
            this.horizontal--;
        }
        setCubeTypeAdvanced();
    }
    public void turnRB(){
        //↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘
        if (getCubeType()==CubeType.RR||getCubeType()==CubeType.B){
            this.vertical++;
        }else if (getCubeType()==CubeType.D){
            //无操作
        }else if (getCubeType()==CubeType.BB||getCubeType()==CubeType.C){
            this.horizontal++;
        }else{
            this.vertical++;
            this.horizontal++;
        }
        setCubeTypeAdvanced();
    }

    public void setCubeTypeAdvanced() {
        int h=getHorizontal();
        int v=getVertical();
        int hmax=getHorizontalMax();
        int vmax=getVerticalMax();
        if (h==0&&v==0){
           setCubeType(CubeType.A);
        }else if (h==0&&v==vmax){
            setCubeType(CubeType.C);
        }else if (h==hmax&&v==0){
            setCubeType(CubeType.B);
        }else if (h==hmax&&v==vmax){
            setCubeType(CubeType.D);
        }else if (h>0&&h<hmax&&v==0){
            setCubeType(CubeType.TT);
        }else if (h>0&&h<hmax&&v==vmax){
            setCubeType(CubeType.BB);
        }else if (v>0&&v<vmax&&h==0){
            setCubeType(CubeType.LL);
        }else if (v>0&&v<vmax&&h==hmax){
            setCubeType(CubeType.RR);
        }else {
            setCubeType(MMM);
        }



    }



}
然后是测试类:

package com.test.sshkeygen;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.xml.internal.bind.v2.model.core.ID;

import java.util.*;

/**
 */
public class RandomartImgTest {
    public static void main(String[] args) {
        char[] charArr={
                ' ','.','o','+','=','*','B','O','X','@','%','&','#','/','^','S','E'
        };
        String s0="09:1d:0f:da:c8:fd:e9:40:53:42:99:76:97:ee:9f:b7";
        String s00="09:1d:27:da:83:dc:fe:94:d0:40:99:76:97:ee:9f:b7";
        String s="fc:94:b0:c1:e5:b0:98:7c:58:43:99:76:97:ee:9f:b7";
        getPic(charArr, s0);
        getPic(charArr, s00);

    }

    private static void getPic(char[] charArr, String s) {
        String [] arr=StringTo16(s);
        String [] arrFinal=StringSplitTo4(arr);
//        ↖ 左上  00
//        ↗ 右上  01
//        ↙ 左下  10
//        ↘ 右下  11
//        horizontal 0-16
//        vertical  0-8
//        数字的值 v*17+h 从76开始 即 h为8,v为4
        Cube c=new Cube(8,4);
        c.setHorizontalMax(16);
        c.setVerticalMax(8);
        c.setCubeType(Cube.CubeType.MMM);
        List<Integer> list=new ArrayList<>();
        list.add(76);
        Set<Integer> uniqueSet = null;

        for (int i=0;i<arrFinal.length;i++) {
            String ss=arrFinal[i];
            if (ss.equals("00")){
                c.turnLT();
            }else if (ss.equals("01")){
                c.turnRT();
            }else if (ss.equals("10")){
                c.turnLB();
            }else if (ss.equals("11")){
                c.turnRB();
            }
//            System.out.println("horizontal:"+c.getHorizontal()+"vertical:"+c.getVertical());
            list.add(c.getHorizontal()+c.getVertical()*(c.getHorizontalMax()+1));
        }
        for (int ilist:list) {
            System.out.println(""+ilist+"");
        }
        uniqueSet = new HashSet(list);
        Integer iMAX = list.get(list.size() - 1);
        Map<Integer,String> map=new HashMap<>();
        for (Integer temp : uniqueSet) {

            if (temp.intValue()==list.get(0).intValue()){
                map.put(temp,"S");
            }else if (temp.intValue()==iMAX.intValue()){
                map.put(temp,"E");
            }else {
                map.put(temp.intValue(),String.valueOf(charArr[Collections.frequency(list, temp)]));
            }
           System.out.print(temp + ": " + Collections.frequency(list, temp)+"|");
        }
        for (Map.Entry entry:map.entrySet()) {
            System.out.println("key:"+entry.getKey()+"value:"+entry.getValue());
        }
        System.out.println("");
        System.out.println("-----------------");
        for (int iVer=0;iVer<=c.getVerticalMax();iVer++){
            List<String>listV=new ArrayList<>();
            for (int iHor=0;iHor<=c.getHorizontalMax();iHor++){
                if (map.containsKey(17*iVer+iHor)){
                    listV.add(map.get(17*iVer+iHor));
                }else{
                    listV.add(" ");
                }
                /*for (Map.Entry entry:map.entrySet()){
                    Integer key= (Integer) entry.getKey();
                    String value= (String) entry.getValue();
                    if (((key/c.getHorizontalMax()-1)==iVer)&&(key%c.getHorizontalMax()==iHor)){
                        listV.add(value);
                    }else{
                        listV.add("~");
                    }
                }*/
            }
            for (String sFinal:listV) {
                System.out.print(sFinal);
            }
            System.out.println("");
        }

        System.out.println("-----------------");

        System.out.println("set size :"+(uniqueSet.size()+1));
        System.out.println("list size :"+(list.size()+1));
        /*验证 正确
        int [] iArr={76, 58, 76, 94, 112, 94, 78, 62, 78, 60, 42, 60, 76, 60, 42, 24, 42,
                26, 10, 26, 44, 26, 8, 26, 42, 24, 40, 24, 40, 22, 40, 58, 42, 24, 40, 24,
                8, 26, 8, 7, 8, 9, 25, 9, 25, 41, 25, 43, 27, 45, 29, 13, 29, 45, 63, 79,
                97, 115, 133, 117, 133, 151, 135, 152, 151};
        for (int idex=0;idex<list.size();idex++) {
            int ii=list.get(idex);
            int iii=iArr[idex];
            if (ii!=iii){
                System.out.println("错误");
            }
            System.out.println(ii==iii);
        }*/
    }

    public static String[] StringSplitTo4(String [] arr){
        String [] arrFinal=new String [arr.length*4];
        for ( int i=0;i<arr.length;i++) {
            String s1=arr[i].substring(6,8);
            String s2=arr[i].substring(4,6);
            String s3=arr[i].substring(2,4);
            String s4=arr[i].substring(0,2);
            arrFinal[4*i]=s1;
            arrFinal[4*i+1]=s2;
            arrFinal[4*i+2]=s3;
            arrFinal[4*i+3]=s4;
        }
        return arrFinal;
    }

    public static String[] StringTo16(String s){
        String[] arr = s.split(":");
        for ( int i=0;i<arr.length;i++) {
            String ss=arr[i];
            String sss=hexString2binaryString(ss);
            arr[i]=sss;
        }
        return arr;
    }


    public static String hexString2binaryString(String hexString)
    {
        if (hexString == null || hexString.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++)
        {
            tmp = "0000"
                    + Integer.toBinaryString(Integer.parseInt(hexString
                    .substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }


}
 
测试数据:
String s0="09:1d:0f:da:c8:fd:e9:40:53:42:99:76:97:ee:9f:b7";
最后结果:
-----------------
       .=o.  .   
     . *+*. o    
      =.*..o     
       o + ..    
        S o.     
         o  .    
          .  . . 
              o .
               E.
-----------------
仅仅知道每个元素出现的位置和次数,就能确定整个路径?这个图能保证路径唯一吗? 我是做后端的,这个我用java代码实现了,但感觉还是有点问题,难道完全寄托于MD5不容易相等的特性?那也不对啊,路径不同可能图是相同的。。。。。

String s0="09:1d:0f:da:c8:fd:e9:40:53:42:99:76:97:ee:9f:b7"; 

String s1="09:1d:27:da:83:dc:fe:94:d0:40:99:76:97:ee:9f:b7"; 

我测过了 这俩的图形一样的  说明了这种方法不可靠,只是看起来直观点而已,还是直接比原始数据靠谱


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Git SSH keygen是用来生成SSH公钥和私钥文件的命令工具。在使用git的时候,我们可以通过执行"ssh-keygen -t rsa -C [youremail.com]"命令来生成SSH密钥对。其,"-t rsa"表示生成RSA算法的密钥对,"-C [youremail.com]"表示在密钥文件添加注释信息,一般是你的邮箱地址。生成的密钥文件会保存在~/.ssh目录下,如果该目录不存在,需要先创建。另外,对于安全性考虑,需要确保~/.ssh目录以及所有父目录的权限不能大于711。通过这个SSH密钥对,我们可以实现gitlab上进行项目的克隆和认证操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [ssh获取公钥,绑定gitlab,ssh-keygen -t rsa命令详解](https://blog.csdn.net/weixin_42752574/article/details/106367959)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [SSHssh-keygen命令基本用法详解](https://download.csdn.net/download/weixin_38659789/14094370)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值