|
| 1 | +/* |
| 2 | + * jPOS Project [http://jpos.org] |
| 3 | + * Copyright (C) 2000-2015 Alejandro P. Revilla |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as |
| 7 | + * published by the Free Software Foundation, either version 3 of the |
| 8 | + * License, or (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 Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.jpos.security; |
| 20 | + |
| 21 | +import org.bouncycastle.util.encoders.Base64; |
| 22 | +import org.jpos.iso.ISOUtil; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | + |
| 27 | +@SuppressWarnings("unused") |
| 28 | +public class SystemSeed { |
| 29 | + public static byte[] getSeed (int l) { |
| 30 | + return getSeed(0, l); |
| 31 | + } |
| 32 | + public static byte[] getSeed (int offset, int l) { |
| 33 | + if (offset + l > seed.length) |
| 34 | + throw new IllegalArgumentException ("Invalid offset/length"); |
| 35 | + byte[] b = new byte[l]; |
| 36 | + System.arraycopy (seed, offset, b, 0, l); |
| 37 | + return b; |
| 38 | + } |
| 39 | + |
| 40 | + private static final byte[] seed; |
| 41 | + static { |
| 42 | + try { |
| 43 | + byte[] _s0 = get("/META-INF/q2/.seed"); |
| 44 | + byte[] _s1 = get("/META-INF/.seed"); |
| 45 | + seed = _s1 == null ? _s0 : ISOUtil.xor(_s0, _s1); |
| 46 | + if (seed == null || seed.length < 16) |
| 47 | + throw new IllegalArgumentException ("Invalid seed"); |
| 48 | + } catch (IOException e) { |
| 49 | + throw new IllegalArgumentException("Invalid system configuration"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static byte[] get (String path) throws IOException { |
| 54 | + InputStream is = SystemSeed.class.getResourceAsStream(path); |
| 55 | + if (is != null) { |
| 56 | + try { |
| 57 | + byte[] b = new byte[is.available()]; |
| 58 | + is.read(b); |
| 59 | + return Base64.decode(b); |
| 60 | + } finally { |
| 61 | + is.close(); |
| 62 | + } |
| 63 | + } |
| 64 | + return null; |
| 65 | + } |
| 66 | +} |
0 commit comments