Coverage Summary for Class: MessageDecoder (co.rsk.net.discovery.message)

Class Class, % Method, % Line, %
MessageDecoder 0% (0/1) 0% (0/3) 0% (0/17)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19  20 package co.rsk.net.discovery.message; 21  22 import co.rsk.net.discovery.PeerDiscoveryException; 23 import org.ethereum.util.FastByteComparisons; 24  25 import static org.ethereum.crypto.HashUtil.keccak256; 26  27 /** 28  * Created by mario on 13/02/17. 29  */ 30 public class MessageDecoder { 31  32  public static final String MDC_CHECK_FAILED = "MDC check failed"; 33  public static final String BAD_MESSAGE = "Bad message"; 34  35  private MessageDecoder() {} 36  37  public static PeerDiscoveryMessage decode(byte[] wire) { 38  if (wire.length < 98) { 39  throw new PeerDiscoveryException(BAD_MESSAGE); 40  } 41  42  byte[] mdc = new byte[32]; 43  System.arraycopy(wire, 0, mdc, 0, 32); 44  45  byte[] signature = new byte[65]; 46  System.arraycopy(wire, 32, signature, 0, 65); 47  48  byte[] type = new byte[1]; 49  type[0] = wire[97]; 50  51  byte[] data = new byte[wire.length - 98]; 52  System.arraycopy(wire, 98, data, 0, data.length); 53  54  int check = check(wire, mdc); 55  56  if (check != 0) { 57  throw new PeerDiscoveryException(MDC_CHECK_FAILED); 58  } 59  60  return PeerDiscoveryMessageFactory.createMessage(wire, mdc, signature, type, data); 61  } 62  63  public static int check(byte[] wire, byte[] mdc) { 64  byte[] mdcCheck = keccak256(wire, 32, wire.length - 32); 65  66  return FastByteComparisons.compareTo( 67  mdc, 68  0, 69  mdc.length, 70  mdcCheck, 71  0, 72  mdcCheck.length); 73  } 74 }